首頁  >  問答  >  主體

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函數並沒有對密碼進行更改,為什麼會有這樣的提示呢?

漂亮男人漂亮男人2732 天前1089

全部回覆(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
  • 取消回覆