【问题标题】:Why does form.validate_on_submit() return false on the first form submission?为什么 form.validate_on_submit() 在第一次提交表单时返回 false?
【发布时间】:2019-11-07 01:39:06
【问题描述】:

我正在使用 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>

【问题讨论】:

  • 您的表单似乎缺少action 属性。您确定示例与您项目中的示例相同吗?
  • 是的,我也有多个没有操作的表单,它们都在第一次提交时起作用。

标签: python flask flask-wtforms flask-login


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-08-19
    • 2014-05-07
    • 1970-01-01
    • 2017-07-27
    • 2011-06-22
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多