2017-03-26 35 views
0

私のアプリケーションで認証を行うFlaskチュートリアルに従っています。私のユーザークラスはそうのようなものです:Python Flask:AttributeError: 'NoneType'オブジェクトに '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 

は私のログインルートがそうのように定義されています。私は/loginエンドポイントに私のブラウザをポイントしたときに

@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' 

ここで何が起こっているのか分かりません。私はGETからエンドポイントを呼び出しています(たとえば、submitはタップされていないので、form.validate_on_submitはfalseにする必要があります)。パスワードチェックは一切行われません。私が間違っていることは何ですか?

答えて

2

check_password_hash()の最初の引数がNoneであるため、例外がスローされます。これは実行されませんでしたUser.password = ...generate_password_hash()Noneを返すことができない)ことを意味し

return check_password_hash(self.password_hash, password) 

:それはあなたが合格最初の引数だとして、あなたのコードでは、それは、self.password_hashNoneであることを意味します。ユーザーにパスワードが設定されていません。

ユーザーにパスワードを渡すか、または防御的にコードを入力し、self.password_hashNoneに設定されているケースを処理します。フォームのテストは常に真ある

注:validate_on_submitは方法、ない財産である

if form.validate_on_submit: 

ので。メソッドオブジェクトは空ではなくゼロでないため、trueです。戻り値が結果を決定

if form.validate_on_submit(): 

:あなたはおそらくコール方法を望んでいました。

+0

if文を正しくネストする方が良いでしょうか?最初にnoneをチェックしてから、パスワードを確認してください。 – KexAri

+0

@KexAri:それは依存します。 'self.password_hash is None'が真のときに何をしたいのですか? –

+0

私は実際に送信ボタンをタップせずにこのルートを呼び出しています。 'form.validate_on_submit'のコードも呼び出されるべきですか?値はfalseなので、そのコードブロックに到達すべきではありません。 – KexAri

関連する問題