搜索

首页  >  问答  >  正文

python - @property装饰器会影响到check_password_hash吗?

用flask来学习做一个博客,代码如下:

# -*- coding:utf-8 -*-
from flask import Flask,render_template,session,redirect,url_for,flash,request
from flask_login import LoginManager,login_required,UserMixin,login_user,logout_user
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.file import FileField
from flask_wtf import Form
from wtforms import StringField,PasswordField,SubmitField,BooleanField
from wtforms.validators import Required
from werkzeug.security import generate_password_hash,check_password_hash
import pymssql

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost:3306/dh_inf_manage'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SECRET_KEY'] = 'hard to guess string'
login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'
login_manager.init_app(app)
db=SQLAlchemy(app)
bootstrap = Bootstrap()
bootstrap.init_app(app)

class NameForm(Form):
    name = StringField(u'用户名:',validators = [Required()])
    password = PasswordField(u'密码:')
    submit = SubmitField('Log In')
    
class LoginForm(Form):
    name = StringField(u'用户名:',validators = [Required()])
    password = PasswordField(u'密码',validators = [Required()])
    submit = SubmitField(u'登陆')
    
class User(UserMixin,db.Model):
    __tablename__ = 'dic_user'
    
    emp_sn = db.Column(db.String(50),primary_key = True)
    name = db.Column(db.String(45))
    password = db.Column(db.String(128))
    emp_dept_sn = db.Column(db.String(45),db.ForeignKey('dic_dept.dept_sn'))
    py_code =  db.Column(db.String(50))
    wb_code =  db.Column(db.String(50))
    autograph = db.Column(db.LargeBinary)
    
    def verify_password(self, password):
        return check_password_hash(self.password, password)
    
    @property 
    def password(self):
        raise AttributeError('password is not a readable attribute')
    
    @password.setter
    def password(self, password):
        self.password = generate_password_hash(password)

    def __repr__(self):
        return '<User %r>' % self.username

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(emp_sn)
    
@app.route ('/',methods=['GET','POST'])
def index():
    return render_template('index.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()   
    if form.validate_on_submit():
        user = User.query.filter_by(emp_sn = form.name.data).first()
        if user is not None and user.verify_password(form.password.data):
            login_user(user)
            return redirect(url_for('index'))
        flash (u'用户名或密码不正确')
    return render_template('login.html',form = form)

@app.route('/logout')
@login_required
def logout():
    logout_user()
    flash(u'你已经注销!')
    return redirect(url_for('index'))
 
@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'),404

@app.errorhandler(500)
def internal_server_error(e):
    return render_template('500.html'),500


if __name__ == '__main__':
    app.run(host = '192.168.199.203',port = 8035,debug = True)

运行时,打开登陆网页,输入用户名的密码后,提示如下错误:

File "C:\flaskr\app\ShiftTurnover.py", line 74, in login
if user is not None and user.verify_password(form.password.data):
File "C:\flaskr\app\ShiftTurnover.py", line 48, in verify_password
return check_password_hash(self.password, password)
File "C:\flaskr\app\ShiftTurnover.py", line 52, in password
raise AttributeError('password is not a readable attribute')
AttributeError: password is not a readable attribute

整个代码,只有

@property
def password(self):
raise AttributeError('password is not a readable attribute')

这里抛出这个错误,login函数并没有对密码进行更改,为什么会有这样的提示呢?

漂亮男人漂亮男人2780 天前1124

全部回复(2)我来回复

  • 迷茫

    迷茫2017-05-18 11:03:09

    真正原因找到了,主要的我数据库的字段password与函数名password冲突了,把函数名称改一下就可以了,我是改为password_hash。

        @property 
        def password_hash(self):
            raise AttributeError('password is not a readable attribute')
        
        @password_hash.setter
        def password_hash(self, password):
            self.password = generate_password_hash(password)

    回复
    0
  • 怪我咯

    怪我咯2017-05-18 11:03:09

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)

    应该是self.password_hash而不是self.password, generate_password_hash是生成密码散列值

    回复
    0
  • 取消回复