【问题标题】:Flask button redirects falsely to index烧瓶按钮错误地重定向到索引
【发布时间】:2018-01-02 19:22:32
【问题描述】:

我有一个应该将内容保存到数据库的输入字段:

{% for comment in comments %}
    <div class="row">
        {{ comment.content }}
    </div>
{% endfor %}

<div class="row">
    <form action="." method="POST">
        <textarea class="form-control" name="contents" placeholder="Enter a comment"></textarea>
        <input type="submit" class="btn btn-warning" value="save">
    </form>
</div>

在 flask_app 我检查它是 POST 还是 GET 方法。如果是 POST,它应该使用重定向重新加载页面。但是,它总是让我回到“家”。

@app.route('/photography', methods=["GET", "POST"])
def show_photography():
    if request.method == "GET":
        return render_template('photography.html', comments=Comment.query.all())
    elif request.method == "POST":
        comment = Comment(content=request.form["contents"])
        db.sesson.add(comment)
        db.session.commmit()
        return 'welcome to flask!'
    else:
        flash('error message via flash')

return 'welcome to flaks' 只是一个测试,单击按钮后它仍然会将我重定向到家。谁能帮帮我?

【问题讨论】:

    标签: python redirect flask


    【解决方案1】:

    看起来你的 'action="."' 是问题所在。在模板中将其设置为 url_for('show_photography')

    【讨论】:

      猜你喜欢
      • 2014-11-13
      • 2017-05-11
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 2023-04-04
      • 2019-02-04
      • 2016-08-17
      相关资源
      最近更新 更多