2017-03-26 90 views
0

我正在關注Flask教程以在我的應用程序中執行身份驗證。我的用戶類是像這樣:當我指出我的瀏覽器到/login端點我收到以下錯誤Python燒瓶:AttributeError:'NoneType'對象沒有'count'屬性

@auth.route('/login', methods=['GET', 'POST']) 
def login(): 
    form = LoginForm() 
    if form.validate_on_submit: 
     # Get a user by email and check password matches hash 
     user = User.query.filter_by(email=form.email.data).first() 
     if user is not None and user.verify_password(form.password.data): 
      login_user(user, form.remember_me.data) 
     flash('Invalid username or password') 
    return render_template('auth/login.html', form=form) 

但是:

File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1994, in __call__ 
return self.wsgi_app(environ, start_response) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1985, in wsgi_app 
response = self.handle_exception(e) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1540, in handle_exception 
reraise(exc_type, exc_value, tb) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1982, in wsgi_app 
response = self.full_dispatch_request() 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1614, in full_dispatch_request 
rv = self.handle_user_exception(e) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1517, in handle_user_exception 
reraise(exc_type, exc_value, tb) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1612, in full_dispatch_request 
rv = self.dispatch_request() 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/flask/app.py", line 1598, in dispatch_request 
return self.view_functions[rule.endpoint](**req.view_args) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/app/auth/views.py", line 14, in login 
if user is not None and user.verify_password(form.password.data): 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/app/models.py", line 38, in verify_password 
return check_password_hash(self.password_hash, password) 
File "/Users/kekearif/Documents/Python/FlaskWebDev/flasky/venv/lib/python2.7/site-packages/werkzeug/security.py", line 245, in check_password_hash 
if pwhash.count('$') < 2: 
AttributeError: 'NoneType' object has no attribute 'count' 

class User(UserMixin, db.Model): # Here we inherit from two classes 
    __tablename__ = 'users' 
    id = db.Column(db.Integer, primary_key=True) 
    email = db.Column(db.String(64), unique=True, index=True) 
    username = db.Column(db.String(64), unique=True, index=True) 
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) 
    password_hash = db.Column(db.String(128)) 

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

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

    def verify_password(self, password): 
     return check_password_hash(self.password_hash, password) 

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

我登錄路由的定義,像這樣

不知道這裏發生了什麼事。我從GET調用端點(例如,提交未被點擊,所以form.validate_on_submit應該是false),並且根本不應該進行密碼檢查。任何想法我做錯了什麼?

回答

2

引發異常是因爲check_password_hash()的第一個參數是None。在你的代碼,這意味着self.password_hashNone,因爲這是你在通過第一個參數:

return check_password_hash(self.password_hash, password) 

這意味着User.password = ...從未執行(generate_password_hash()不能返回None)。沒有爲您的用戶設置密碼。

防禦地給用戶一個密碼或代碼,並處理self.password_hash設置爲None的情況。

請注意,您的形式測試總是正確的

if form.validate_on_submit: 

因爲validate_on_submit是一種方法,而不是一個屬性。方法對象非空且非零,所以如此。你可能想呼叫方法:

if form.validate_on_submit(): 

現在的返回值決定勝負。

+0

嵌套if語句會更好嗎?首先檢查沒有,然後檢查密碼。 – KexAri

+0

@KexAri:它取決於;當'self.password_hash是None'時,你想要做什麼? –

+0

我實際上調用這條路線而不點擊提交按鈕。是否應該調用'form.validate_on_submit'中的代碼?該值爲假,因此它不應該達到該代碼塊。 – KexAri

相關問題