Flask WTForms 自定义验证器始终显示 'Username or password is incorrect'
Flask WTForms custom validator is always showing 'Username or password is incorrect'
我正在尝试为登录表单设置自定义验证器,但它总是显示 'username and password are incorrect' 即使我输入了有效验证器。有什么建议么?
这是我的代码:
def login_check(form,field):
user_email = User.query.filter_by(email=form.email.data).first()
if user_email is None:
raise ValidationError('Username or password is incorrect')
elif not check_password_hash(field.data, user_email.password):
raise ValidationError('Username or password is incorrect')
class LoginForm(FlaskForm):
email = StringField('Email', validators=[InputRequired(), Email()])
password = PasswordField('Password', validators=[InputRequired(),login_check])
submit = SubmitField('Login')
我用 passlib.hash pbkdf2_sha256 替换了 werkzeug.security 现在它可以工作了,我不知道为什么会这样
代码
from passlib.hash import pbkdf2_sha256
def login_check(form,field):
user_email = User.query.filter_by(email=form.email.data).first()
if user_email is None:
raise ValidationError('Username or password is incorrect')
elif not pbkdf2_sha256.verify(field.data, user_email.password):
raise ValidationError('Username or password is incorrect')
路线
@app.route('/register', methods=['POST','GET'])
def register():
reg_form = RegistrationForm()
if reg_form.validate_on_submit():
password = reg_form.password.data
hased_pass = pbkdf2_sha256.hash(password)
user = User(name=reg_form.name.data, lastname=reg_form.lastname.data,
email=reg_form.email.data, password=hased_pass)
db.session.add(user)
db.session.commit()
login_user(user)
flash(f'Welcome {user.name}, Your account have been created')
return redirect(url_for('index'))
我正在尝试为登录表单设置自定义验证器,但它总是显示 'username and password are incorrect' 即使我输入了有效验证器。有什么建议么? 这是我的代码:
def login_check(form,field):
user_email = User.query.filter_by(email=form.email.data).first()
if user_email is None:
raise ValidationError('Username or password is incorrect')
elif not check_password_hash(field.data, user_email.password):
raise ValidationError('Username or password is incorrect')
class LoginForm(FlaskForm):
email = StringField('Email', validators=[InputRequired(), Email()])
password = PasswordField('Password', validators=[InputRequired(),login_check])
submit = SubmitField('Login')
我用 passlib.hash pbkdf2_sha256 替换了 werkzeug.security 现在它可以工作了,我不知道为什么会这样
代码
from passlib.hash import pbkdf2_sha256
def login_check(form,field):
user_email = User.query.filter_by(email=form.email.data).first()
if user_email is None:
raise ValidationError('Username or password is incorrect')
elif not pbkdf2_sha256.verify(field.data, user_email.password):
raise ValidationError('Username or password is incorrect')
路线
@app.route('/register', methods=['POST','GET'])
def register():
reg_form = RegistrationForm()
if reg_form.validate_on_submit():
password = reg_form.password.data
hased_pass = pbkdf2_sha256.hash(password)
user = User(name=reg_form.name.data, lastname=reg_form.lastname.data,
email=reg_form.email.data, password=hased_pass)
db.session.add(user)
db.session.commit()
login_user(user)
flash(f'Welcome {user.name}, Your account have been created')
return redirect(url_for('index'))