1.更新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

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)

 

@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(\'/denglu/\', methods=[\'GET\', \'POST\'])
def denglu():
    if request.method == \'GET\':
        return render_template("denglu.html")
    else:
        username = request.form.get(\'username\')
        password1 = request.form.get(\'password\')
        user = User.query.filter(User.username == username).first()
        if user:
            if user.check_password(password1):
                session[\'user\']=username
                session[\'userid\']=user.id
                session.permanent = True
                return redirect(url_for(\'index\'))
            else:
                return \'密码错误\'
        else:
            return \'用户名不存在\'

 

版权声明:本文为892356821qqcom原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/892356821qqcom/p/8085216.html