【问题标题】:403 Forbidden POST - Flask WTForms403 禁止 POST - Flask WTForms
【发布时间】:2014-04-13 20:05:20
【问题描述】:

我有一个在 Google App Engine 上运行的 Flask 应用程序。我正在尝试提交使用 WTForm 构建的表单,但我不断收到以下错误。

403 Forbidden:您无权访问请求的资源。服务器要么读保护要么不可读。

Location.html(部分代码)

    <form method=post action="/home/location">
    ....
     <button type="submit" class="btn btn-primary" value="Submit">
                        Directions
                    </button>
    </form>

main.py

@app.route('/home/location', methods=['POST', 'GET'])
def location():
    form = cfcdirections.Direction(request.form)
    print(request.method)
    if request.method == 'POST' and form.validate():
        print("After if")
        directions = cfcdirections()
        street_no = directions.No
        street = directions.Street
        suburb = directions.Suburb
        postcode = directions.Postcode
        state = directions.State
    return render_template('Location.html',form=form)

【问题讨论】:

  • 您的表单中是否包含CSRF token
  • 我正在使用 Flask 扩展 @app.before_request def csrf_protect(): if request.method == "POST": token = session.pop('_csrf_token',None) if not token or token != request.form.get('_csrf_token'): abort(403)
  • 是的,在您的模板中,您是否包含令牌?
  • 啊……是的,我明白了。我没有将其包含在此表单中。
  • @MartijnPieters 我想你只需要在答案中发布这个:{{form.csrf_token}} :)

标签: google-app-engine flask flask-wtforms


【解决方案1】:

不要忘记在表单中包含CSRF token

<form method=post action="/home/location">
    {{ form.csrf_token }}
    ....
    <button type="submit" class="btn btn-primary" value="Submit">
                    Directions
                </button>
</form>

【讨论】:

    猜你喜欢
    • 2019-04-01
    • 2018-11-06
    • 2017-03-12
    • 2021-10-03
    • 1970-01-01
    • 2018-05-18
    • 2019-03-27
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多