2017-08-29 26 views
0

我正在構建一個使用zappa部署到AWS Lambda的應用程序,我試圖使用Flask-s3來處理靜態文件。我從來沒有使用[瓶-S3] [1]之前,它似乎相當簡單,但我發現......AttributeError:'FlaskS3'對象沒有屬性'url_for'

AttributeError: 'FlaskS3' object has no attribute 'url_for'

他們這樣,我理解你只需要更換你的靜態的URL url_for像這樣:

app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname' 
s3 = FlaskS3(app) 

s3.url_for('static/file.jpg') 

這不是工作,所以我清楚我做錯了什麼,但有幾乎沒有在線故障排除瓶-S3。任何幫助。

app = Flask(__name__) 
Bootstrap(app) 
app.config['FLASKS3_BUCKET_NAME'] = 'mybucketname' 
s3 = FlaskS3(app) 
MAILGUN_API_KEY = 'key' 
auth = ('api', MAILGUN_API_KEY) 


@app.route("/", methods=['GET', 'POST']) 
def index(): 
    context = { 
    'image': url_for('static/property_home.jpeg'), 
    'heading': 'We sell property - At a discount!', 
    'landing_video': url_for('static/intro.mp4') 
    } 
    form = forms.OptIn() 
    if form.validate_on_submit(): 
     validate = requests.get(
      "https://api.mailgun.net/v3/address/private/validate", 
      auth=auth, 
      params={"address": form.email.data}) 
     if validate.json()['did_you_mean'] is not None: 
      flash('Did you mean {}?'.format(validate.json()['did_you_mean'])) 
     elif validate.json()['is_valid'] == True and validate.json()['is_role_address'] == False and validate.json()['is_disposable_address'] == False: 
      r = requests.post(
      "https://api.mailgun.net/v3/lists/YOUR_DOMAIN_NAME/members", 
      auth=auth, 
      data={'subscribed': True, 
        'address': form.email.data}) 
      if r.status_code == 200: 
       requests.post('https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages', 
        auth=auth, 
        data={"from": '[email protected]', 
         "to": form.email.data, 
         "subject": "Welcome to Spokane Discount Properties", 
         "html": open('templates/indoc.html'), 
         "o:tag": 'indoctrinated'}) 
       flash('Thanks, we will notify you when we have more properties') 
      else: 
       flash('You are already subscribed, we will notify you when more properties are available') 
     else: 
      flash('Holy guacamole! Best check yo self, this is not a valid email.') 
    return render_template('index.html', form=form, context=context) 

回答

0

doc here

In terms of getting your application to use external Amazon S3 URLs when referring to your application’s static assets, passing your Flask object to the FlaskS3 object is all that needs to be done.

擴展需要處理的url_for爲您服務。所以你可能不需要直接調用它。

Internally, every time url_for is called in one of your application’s templates, flask_s3.url_for is instead invoked. If the endpoint provided is deemed to refer to static assets, then the S3 URL for the asset specified in the filename argument is instead returned. Otherwise, flask_s3.url_for passes the call on to flask.url_for.

改變這一點:

context = { 
    'image': url_for('static/property_home.jpeg'), 
    'heading': 'We sell property - At a discount!', 
    'landing_video': url_for('static/intro.mp4') 
    } 

到:

context = { 
    'image': url_for('static', filename= 'property_home.jpeg'), 
    'heading': 'We sell property - At a discount!', 
    'landing_video': url_for('static', filename='intro.mp4') 
    } 
+0

感謝@Mekicha但我已經嘗試了使用'本身url_for'。然後我得到'BuildError:無法建立端點的URL' – freefly0313

+0

我需要看到你的代碼能夠幫助。大多數情況下'url_for'都需要一個函數。 – Mekicha

+0

再次感謝。我爲你添加了我的代碼 – freefly0313