我有一個用戶模型,具有使用Werkzeug的set_password_hash
和check_password_hash
函數設置和檢查密碼的功能。爲什麼check_password_hash在這個登錄視圖中總是返回False?
models.py
class User(db.Model):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(15), unique = True)
password = db.Column(db.String(15))
tasks = db.relationship('Task', backref='author', lazy='dynamic')
def __init__(self, username, password):
self.username = username
self.set_password(password)
def set_password(self, password):
self.password = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password, password)
login.py
def post(self):
username = flask.request.form['username']
passwd = flask.request.form['passwd']
user = User.query.filter_by(username = username).first()
flask.flash(user.check_password(user.password))
if user and user.check_password(user.password):
flask.session['username'] = username
else:
flask.flash("User doesn't exist or password is inccorect.")
return flask.render_template('login.html')
return flask.render_template('index.html')
的check_password
閃蒸結果始終是 「假」。我究竟做錯了什麼?
我這樣傳遞:'new_user = User(username,password)'然後''set_password''函數對它進行加密。 – user3448282
'from werkzeug import generate_password_hash,check_password_hash' – user3448282