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)#内部使用 nickname = db.Column(db.String(20)) @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 复制代码 复制代码 @app.route(‘/login/‘, methods=[‘GET‘, ‘POST‘]) def login(): if request.method == ‘GET‘: return render_template("login.html") else: usern = request.form.get(‘username‘) password1 = request.form.get(‘password‘) user = User.query.filter(User.username == usern).first() if user: if user.check_password(password1): session[‘user‘] = usern session[‘id‘] = user.id session.permanent = True return redirect(url_for(‘index‘)) else: return ‘密码错误‘ else: return ‘用户名不存在‘