我正在阅读《 Flask Web开发》一书,但遇到以下代码问题:

class RegistrationForm(Form):
    email = StringField('Email', validators=[Required(),
                        Length(1, 64), Email()])
    username = StringField('Username', validators=[Required(),
                           Regexp('^[A-Za-z][A-Za-z0-9_.]*$', 0,
                           'Usernames must have only letters, numbers,'
                                  'underscores or dots')])
    password = PasswordField('Password', validators=[Required(),
                             EqualTo(password2,
                             message="Passwords must match")])
    password2 = PasswordField('Confirm password', validators=[Required()])
    submit = SubmitField('Register')


password2之后定义的书password中,这会导致错误:NameError: name 'password2' is not defined。这里发生了什么?我应该如何定义password2

最佳答案

引用它

password = PasswordField('Password', validators=[Required(),
                         EqualTo('password2',
                         message="Passwords must match")])

08-27 09:24