我正在關注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),並且根本不應該進行密碼檢查。任何想法我做錯了什麼?
嵌套if語句會更好嗎?首先檢查沒有,然後檢查密碼。 – KexAri
@KexAri:它取決於;當'self.password_hash是None'時,你想要做什麼? –
我實際上調用這條路線而不點擊提交按鈕。是否應該調用'form.validate_on_submit'中的代碼?該值爲假,因此它不應該達到該代碼塊。 – KexAri