Flask消息闪烁错误-flask.debughelpers.FormDataRoutingRedirect [英] Flask Message Flashing error - flask.debughelpers.FormDataRoutingRedirect

查看:84
本文介绍了Flask消息闪烁错误-flask.debughelpers.FormDataRoutingRedirect的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行烧瓶代码时,我当前收到以下错误消息:

I am currently getting the following error when I run my flask code:

flask.debughelpers.FormDataRoutingRedirect-FormDataRoutingRedirect:已将请求发送到此URL( http://localhost:5000/login ),但是重定向由路由系统自动发布到" http://localhost:5000/login/".该URL定义了一个斜杠,因此如果没有访问Flask,它将自动重定向到带有斜杠的URL.请确保直接将您的POST请求发送到此URL,因为我们无法使浏览器或HTTP客户端可靠地重定向表单数据或在没有用户交互的情况下进行重定向.

flask.debughelpers.FormDataRoutingRedirect - FormDataRoutingRedirect: A request was sent to this URL (http://localhost:5000/login) but a redirect was issued automatically by the routing system to "http://localhost:5000/login/". The URL was defined with a trailing slash so Flask will automatically redirect to the URL with the trailing slash if it was accessed without one. Make sure to directly send your POST-request to this URL since we can't make browsers or HTTP clients redirect with form data reliably or without user interaction.

我认为这与闪烁消息的位置有关,但我似乎不知道该如何解决.我是Flask的新手,因此无法完全确定解决此问题的最佳选择.

I think it is to do with where the message flashing is placed but I can't seem to figure out how to fix this. I’m new to flask so not entirely sure what the best option is to solve this issue.

我已经包含了index.py和login.html文件,以帮助重现此错误.先谢谢您的帮助!:)

I have included my index.py and login.html file to help reproduce this error. Thanks in advance for the help! :)

@app.route('/login/', methods=['GET', 'POST'])
def login():
    form = LoginForm()

    if form.validate_on_submit():
        user = User.query.filter_by(username=form.username.data).first()
        if user:
            if check_password_hash(user.password, form.password.data):
                login_user(user, remember=form.remember.data)
                flash("Invalid username or password")
                return redirect(url_for('dashboard'))

    return render_template('login.html', form=form, errors=form.errors.items())

Login.html模板

{% block body_content %}
<div class="container">

<form method="POST" action="/login">
    {{ form.csrf_token }}
    <div class="form-group">
       {{ form.username }}
    </div>
    <div class="form-group">
        {{ form.password }}
    </div>
    <div class="form-group">
        {{ form.remember }}
    </div>
    <button type="submit" class="btn btn-primary">Sign In</button>
</form>

<div class="errors">
    {% for field, err in errors %}
        <p>{{field}} : {{err|join(', ')}}</p>
    {% endfor %}
</div>

<div class="message-flash">  
    {% for message in get_flashed_messages() %}
      <div class=flash>{{ message }}</div>
    {% endfor %}
</div> 

</div>
{% endblock %}

推荐答案

按照消息中的建议,在表单内的 action 属性中添加斜杠:

Add a trailing slash to the action attribute inside the form as suggested in the message:

<form method="POST" action="/login/">

最好使用 url_for 构建URL:

<form method="POST" action="{{ url_for('login') }}">

由于您使用与帖子相同的视图来呈现表单,因此可以省略 action .

Since you're rendering the form with the same view that handles the post, you can omit the action.

<form method="POST">

这篇关于Flask消息闪烁错误-flask.debughelpers.FormDataRoutingRedirect的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆