내 앱에서 인증을 수행하기 위해 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
내 로그인 경로과 같이 정의된다 :
@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)
을 그러나 나는 다음과 같은 오류가 얻을 /login
엔드 포인트에 브라우저를 가리킬 때 :
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에서 호출하고 있습니다 (예 : 제출이 탭되지 않았으므로 form.validate_on_submit이 false 여야 함). 암호 확인이 전혀 수행되지 않아야합니다. 내가 뭘 잘못하고 있는거야?
if 문을 중첩하는 것이 더 좋을까요? 먼저 아무도 확인하지 않은 다음 암호를 확인하십시오. – KexAri
@KexAri : 그것이 다릅니다; 'self.password_hash is None'이 참일 때 당신이하고 싶은 것은 무엇입니까? –
실제로 제출 버튼을 탭하지 않고이 경로를 호출하고 있습니다. 'form.validate_on_submit'의 코드도 호출해야합니까? 값은 false이므로 해당 코드 블록에 도달해서는 안됩니다. – KexAri