WTForm验证始终失败,验证器不会生成错误消息

2024-03-28 19:53:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我曾经在屏幕上输入了错误的信息,但是我没有错误的输入

我也试着输入正确的东西来尝试,但是一旦我用print检查,它表明验证总是失败的。(已解决)

应用程序:

@app.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        print('validated')
        username = form.username.data
        password = bcrypt.generate_password_hash(form.password.data)
        email = form.mail.data
        print(username, password, email)
        flash("Successfully registered")
    else:
        print('validation failed')
    return render_template('register.html', form=form)

注册执行:

^{pr2}$

html格式:

{% extends 'base.html' %}
{% import "bootstrap/wtf.html" as wtf %}

{% block content %}
    <div class = 'container'>
        <br>
        <h1>Register Now</h1>
        <br>
        <div class="row">
            <div class="col-md-8">
                {{ form.hidden_tag() }}
                {{ wtf.form_errors(form, hiddens="only") }}

                {{ wtf.form_field(form.username) }}
                {{ wtf.form_field(form.password) }}
                {{ wtf.form_field(form.confirm_password) }}
                {{ wtf.form_field(form.email) }}
                {{ wtf.form_field(form.submit) }}
            </div>
        </div>
    </div>
{% endblock %}

我也试过了

{{ wtf.quick_form(form) }}

但也不会生成错误消息。在


Tags: brdivformregisterfielddataemailhtml
1条回答
网友
1楼 · 发布于 2024-03-28 19:53:01

你试过了吗

{{ wtf.form_errors(form) }}

根据docs

hiddens – If True, render errors of hidden fields as well. If 'only', render only these.

所以只显示隐藏字段的错误。在

相关问题 更多 >