【问题标题】:AttributeError: 'FlaskS3' object has no attribute 'url_for'AttributeError:“FlaskS3”对象没有属性“url_for”
【发布时间】:2018-02-06 10:30:38
【问题描述】:

我正在构建一个使用 zappa 部署到 AWS Lambda 的烧瓶应用程序,并且我正在尝试使用 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)

【问题讨论】:

    标签: python amazon-web-services flask attributeerror url-for


    【解决方案1】:

    来自doc here

    在让您的应用程序使用外部 Amazon S3 URL 方面 当引用您的应用程序的静态资产时,传递您的 Flask FlaskS3 对象的对象是所有需要做的。

    该扩展程序会为您处理url_for。所以你可能不需要直接调用它。

    在内部,每次 url_for 在您的应用程序之一中被调用 模板,而是调用 flask_s3.url_for。如果端点 提供被认为是指静态资产,那么 S3 URL 为 而是返回文件名参数中指定的资产。 否则,flask_s3.url_for 会将调用传递给 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')
        }
    

    【讨论】:

    • 谢谢@Mekicha,但我已经尝试过单独使用url_for。然后我得到BuildError: Could not build url for endpoint
    • 我需要查看您的代码才能提供帮助。 url_for 大多期待函数。
    • 更新了我的答案。对于静态文件,你必须这样做。
    • 非常感谢!完美运行
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多