为什么form.validate_on_submit()在第一次提交表单时返回false?

我正在使用wtforms创建一个登录表单,但是该登录表单在首次提交时不起作用。当您第二次提交它时,form.validate_on_submit()返回true。

这是我的登录视图:

@app.route("/login",methods=['GET','POST'])
def login():
    if current_user.is_authenticated:
        return redirect(url_for('admin'))
    form = LoginForm()
    if form.validate_on_submit():
        user = User.query.first()
        if user and bcrypt.check_password_hash(user.password,form.password.data):
            login_user(user)
            redirect(url_for('admin'))
    return render_template('login.html',form=form)

这是我的表单html:

<form method="POST">
    {{ form.hidden_tag() }}
    <div class="field">
        <div class="control">
            {{ form.password(class="input is-large",placeholder="Your Password") }}
        </div>
    </div>
    {{ form.submit(class="button is-block is-success is-large is-fullwidth") }}
</form>
cj130555 回答:为什么form.validate_on_submit()在第一次提交表单时返回false?

我刚刚修复了该错误,在我的代码中我不小心键入了redirect(url_for('admin'))而不是return redirect(url_for('admin'))

本文链接:https://www.f2er.com/3148024.html

大家都在问