作者:人一旦_488 | 来源:互联网 | 2023-10-12 12:43
验证用户名和密码,validate_on_validate()返回值因为True,但是下面的代码却是False,求解答:
1 2 3 4 5 6 7 8 9 10
| class LoginForm(Form):
username = StringField('Username')
password = PasswordField('Password')
submit = SubmitField('Login')
def validate_username(self, field):
if field.data != 'you':
raise ValidationError('Invalid username')
def validate_password(self, field):
if field.data != 'flask':
raise ValidationError('Invalid password') |
1 2 3 4 5 6 7
| @app.route('/', methods=['POST', 'GET'])
def login_view():
form = LoginForm()
if form.validate_on_submit():
flash('Username and password are correct')
return redirect('/')
return render_template('form.html', form = form) |
如上form.validate_on_submit()返回值总是为False
我只能把代码改为:
1 2 3 4 5 6 7 8 9
| @app.route('/', methods=['POST', 'GET'])
def login_view():
form = LoginForm()
if request.method == 'POST':
if not form.validate_on_submit():
flash('Username and password are correct')
return redirect('/')
else:
return render_template('form.html', form = form) |
这样才会有用,求解答