我在我的 Flask 应用程序中使用了 flask_form 并且现在已经被“CSRF 令牌不匹配”卡住了几个小时。
<form method="post" action="{{ url_for('auth.login') }}" role="form">
{{ form.hidden_tag() }}
{{ wtf.form_errors(form, hiddens="only") }}
{{ wtf.form_field(form.email)}}
{{ wtf.form_field(form.password)}}
<p><button type="submit">Login</button></p>
</form>
Run Code Online (Sandbox Code Playgroud)
视图.py
@auth.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
print('login form received on server and is valid')
# check whether user exists in the database and whether
# the password entered matches the password in the database
user = User.query.filter_by(email=form.email.data).first()
if user is not None and user.verify_password(form.password.data) and check_password_hash(user.pwd, form.password.data):
# log …Run Code Online (Sandbox Code Playgroud)