greyli / helloflask

Hello, Flask!
https://docs.helloflask.com
MIT License
1.87k stars 2.53k forks source link

form.validate_on_submit() 总是返回false #152

Closed baichen99 closed 5 years ago

baichen99 commented 5 years ago

代码如下

# forms.py
class RegisterForm(FlaskForm):
    school_id = IntegerField("学号", validators=[DataRequired(message="请输入数据")])
    username = StringField("username", validators=[DataRequired(message="请输入数据")])
    password = PasswordField("password", validators=[DataRequired(message="请输入数据")])
    repeat_password = PasswordField('repeat', validators=[EqualTo(password, message="两次输入的密码不一致")])
    submit = SubmitField("submit")
# /blueprints/auth.py
@auth_bp.route('/register', methods=['GET', 'POST'])
def register():
    form = RegisterForm()
    if form.validate_on_submit():
        school_id = form.school_id.data
        username = form.username.data
        return 'success'
    return render_template('register.html', form=form)

debug发现form中_unboundFiled属性为

<class 'list'>: [('school_id', <UnboundField(IntegerField, ('学号',), {'validators': [<wtforms.validators.DataRequired object at 0x10e1c2128>]})>), ('username', <UnboundField(StringField, ('username',), {'validators': [<wtforms.validators.DataRequired object at 0x10e1c2198>]})>), ('password', <UnboundField(PasswordField, ('password',), {'validators': [<wtforms.validators.DataRequired object at 0x10e1c2208>]})>), ('repeat_password', <UnboundField(PasswordField, ('repeat',), {'validators': [<wtforms.validators.EqualTo object at 0x10e1c22b0>]})>), ('submit', <UnboundField(SubmitField, ('submit',), {})>)]

不知道是不是这个问题

baichen99 commented 5 years ago
# tempates/register.html

<form action="{{ url_for("auth.register") }}" method="post">
            {{ form.csrf_token }}
            <img src="http://via.placeholder.com/100x100/aaa/fff?text=IMG" alt="" class="center-block register-img">
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-equalizer"></span></span>
                {{ form.school_id.label(class="sr-only") }}
                {{ form.school_id(class="form-control", placeholder="学号") }}
            </div>
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-user"></span></span>
                {{ form.username.label(class="sr-only") }}
                {{ form.username(class="form-control", placeholder="用户名") }}
            </div>
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
                {{ form.password.label(class="sr-only") }}
                {{ form.password(class="form-control", placeholder="密码") }}
            </div>
            <div class="input-group">
                <span class="input-group-addon"><span class="glyphicon glyphicon-asterisk"></span></span>
                {{ form.repeat_password.label(class="sr-only") }}
                {{ form.repeat_password(class="form-control", placeholder="重复密码") }}
            </div>
            {{ form.submit(class="btn btn-primary input-sm col-sm-12") }}
        </form>
baichen99 commented 5 years ago

已解决,是EqualTo验证器的参数填写错误