WTForms不验证输入

时间:2019-07-31 09:13:42

标签: python-3.x

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import InputRequired, Length, Email, EqualTo

class RegistrationForm(FlaskForm):
    username = StringField("Username", validators=[InputRequired(), Length(min=2, max=10)])
    email = StringField("Email", validators=[InputRequired(), Email()])
    password = PasswordField("Password", validators=[InputRequired()])
    confirm_password = PasswordField("Confirm Password", validators=[InputRequired(), EqualTo(password)])
    remember = BooleanField("Remember Me")
    submit = SubmitField("Sign Up")



@app.route("/register/", methods = ['GET', 'POST'])
def register():
    form = RegistrationForm()
    if form.validate_on_submit():
        flash('Account created for ' + form.username.data + '!', 'success')
        return redirect(url_for('home'))
    else:
        print(form.errors)
        print("not valid")
    return render_template("register.html", title = "Register", form = form)

'''它产生以下错误: {'email':['此字段为必填。'],'confirm_password':[“无效的字段名']})>'。”]}'''

''' 我对Flask和python还是陌生的,所以,如果问题很明显,我会提前道歉。谢谢你的时间。'''

1 个答案:

答案 0 :(得分:0)

您似乎正在关注Corey schafer的FLASK教程(它(和Corey Schafer)是一个很好的资源)。

在我的代码中,我使用DataRequired()而不是InputRequired(),并且效果很好。让我知道您是否仍在努力。