AttributeError: 'FlaskS3' object has no attribute 'url_for'
AttributeError: 'FlaskS3' object has no attribute 'url_for'
我正在构建一个使用 zappa 部署到 AWS Lambda 的 Flask 应用程序,我正在尝试使用 Flask-s3 来处理静态文件。我以前从未使用过 [Flask-S3][1],它看起来相当简单,但我正在...
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')
这显然没有工作,我做错了什么,但几乎没有在线故障排除 Flask-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": 'Matt@SpokaneDiscountProperties.com',
"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)
来自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')
}
我正在构建一个使用 zappa 部署到 AWS Lambda 的 Flask 应用程序,我正在尝试使用 Flask-s3 来处理静态文件。我以前从未使用过 [Flask-S3][1],它看起来相当简单,但我正在...
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')
这显然没有工作,我做错了什么,但几乎没有在线故障排除 Flask-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": 'Matt@SpokaneDiscountProperties.com',
"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)
来自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')
}