Flask Message Flashing error - flask.debughelpers.FormDataRoutingRedirect
运行烧瓶代码时,当前出现以下错误:
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.
号
我认为这与消息闪烁的位置有关,但我似乎不知道如何解决这个问题。我对烧瓶不太熟悉,所以不完全确定解决这个问题的最佳选择是什么。
我已经包含了index.py和login.html文件来帮助重现这个错误。提前感谢您的帮助!:)
app.py的登录部分1 2 3 4 5 6 7 8 9 10 11 12 13 | @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模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | {% block body_content %} <form method="POST" action="/login"> {{ form.csrf_token }} {{ form.username }} {{ form.password }} {{ form.remember }} <button type="submit" class="btn btn-primary">Sign In</button> </form> {% for field, err in errors %} <p> {{field}} : {{err|join(', ')}} </p> {% endfor %} {% for message in get_flashed_messages() %} {{ message }} {% endfor %} {% endblock %} |
号
按照消息中的建议,在表单内的
1 | <form method="POST" action="/login/"> |
最好使用
1 | <form method="POST" action="{{ url_for('login') }}"> |
号
由于您使用处理日志的相同视图呈现表单,因此可以省略
1 | <form method="POST"> |