1.更新User對象,設置對內的_passwordhtml
class User(db.Model):session
__tablename__ = 'user' app
_password = db.Column(db.String(200), nullable=False) #內部使用url
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(20), nullable=False) nickname = db.Column(db.String(20), nullable=False) userphone = db.Column(db.String(20), nullable=False) _password = db.Column(db.String(200), nullable=False)#內部使用
2.編寫對外的passwordspa
from werkzeug.security import generate_password_hash, check_password_hashcode
@propertyorm
def password(self): #外部使用,取值htm
return self._password
對象
@password.setterblog
def password(self, row_password):#外部使用,賦值
self._password = generate_password_hash(row_password)
from werkzeug.security import generate_password_hash, check_password_hash
@property def password(self): #外部使用 return self._password @password.setter def password(self, row_password): self._password = generate_password_hash(row_password)
3.密碼驗證方法:
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
4.登陸驗證:
password1 = request.form.get('password')
user = User.query.filter(User.username == username).first()
if user:
if user.check_password(password1):
@app.route('/deng', methods=['GET', 'POST']) def deng(): if request.method == 'GET': return render_template('deng.html') else: username = request.form.get('username') password = request.form.get('password') user = User.query.filter(User.username == username).first() if user: if user.check_password(password): session['user'] = username session.permanent = True return redirect(url_for('admin')) else: return 'password error' # return redirect(url_for('admin')) else: return 'error username or password'