gpt4 book ai didi

Python flask : AttributeError: 'NoneType' object has no attribute 'count'

转载 作者:行者123 更新时间:2023-12-03 09:10:24 32 4
gpt4 key购买 nike

我正在按照 Flask 教程在我的应用程序中进行身份验证。我的用户类别是这样的:

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),并且根本不应该进行密码检查。有什么想法我做错了吗?

最佳答案

抛出异常是因为 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():

现在返回值决定结果。

关于Python flask : AttributeError: 'NoneType' object has no attribute 'count' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43030956/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com