1.更新User对象,设置对内的_password
1 class User(db.Model): 2 3 __tablename__ = ‘user‘ 4 5 _password = db.Column(db.String(200), nullable=False) #内部使用
2.编写对外的password
1 from werkzeug.security import generate_password_hash, check_password_hash 2 3 @property 4 5 def password(self): #外部使用,取值 6 7 return self._password 8 9 10 @password.setter 11 12 def password(self, row_password):#外部使用,赋值 13 14 self._password = generate_password_hash(row_password)
3.密码验证方法:
1 def check_password(self, row_password): #密码验证 2 3 result = check_password_hash(self._password,row_password) 4 5 return result
4.登录验证:
1 password1 = request.form.get(‘password‘) 2 3 user = User.query.filter(User.username == username).first() 4 5 if user: 6 7 if user.check_password(password1):