更新User对象,设置对内的_password
class User(db.Model):
__tablename__ = 'user'
_password = db.Column(db.String(200), nullable=False) #内部使用
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(20), nullable=False)
_password = db.Column(db.String(200), nullable=False)#内部使用
2.编写对外的password
@property
def password(self):#外部使用
return self._password
@password.setter
def password(self,row_password):
self._password=generate_password_hash(row_password)
def check_password(self,row_password):
result=check_password_hash(self._password,row_password)
return result
.密码验证方法:
def check_password(self,row_password):
result=check_password_hash(self._password,row_password)
return result
.登录验证:
if user:
if user.check_password(password) :
session['user'] = username
session['userid']=user.id
session.permanent = True
return redirect(url_for('shouye'))
else:
return u'password error.'
else:
return u'username is not existed.'