烧瓶错误“ NoneType”对象没有属性“密码”

时间:2019-09-30 10:38:11

标签: python flask-sqlalchemy flask-login

class Register(db.Model):
    __tablename__ = 'register'
    register_id = db.Column(db.Integer, primary_key=True)
    firstname = db.Column(db.String(50), nullable=False)
    lastname = db.Column(db.String(50), nullable=False)
    gender = db.Column(db.String(20), nullable=False)
    occupation = db.Column(db.String(20), nullable=False)
    dateofbirth = db.Column(db.String(50), nullable=True)
    address = db.Column(db.String(255), nullable=False)
    contact = db.Column(db.String(20), nullable=False)
    email_id = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(20), nullable=False)

这是登录功能

@app.route("/login",methods=['GET','POST'])
def login_post():
    email = request.form.get('email')
    password = request.form.get('pass')
    user = Register.query.filter_by(email_id=email).first()
    print("password",password)
    print("email",email)
    print(user.password)
    if not user or not check_password_hash(user.password, password):
        flash('Please check your login details and try again.')
        return redirect(url_for('register'))
    login_user(user)
    return redirect(url_for('index'))
    return render_template('login.html')

这是错误

    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 1866, in 
    handle_exception
    reraise(exc_type, exc_value, tb)
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request() 
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 1951, in 
    full_dispatch_request
    rv = self.handle_user_exception(e)
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 1820, in 
    handle_user_exception
    reraise(exc_type, exc_value, tb)
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 1949, in 
    full_dispatch_request
    rv = self.dispatch_request()
    File "C:\Users\dhruv\Desktop\Farmer\venv\lib\site-packages\flask\app.py", line 1935, in 
    dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
    File "C:\Users\dhruv\Desktop\Farmer\Main.py", line 46, in login_post
    print(user.password)
    AttributeError: 'NoneType' object has no attribute 'password'

如何解决?我正在创建登录身份验证,其中该行user = Register.query.filter_by(email_id=email).first()给了我这个错误。这是什么问题? 是因为浏览器的Cookie吗?或任何与python版本?我正在使用python 3.7最新版本并在localhost上的pycharm中进行编码

1 个答案:

答案 0 :(得分:-1)

首先,检查您的模型是否完美,并确保您的用户表中至少有一条记录。

然后删除浏览器 cookie 并尝试。

如果你仍然面临同样的问题,试试这个:

转到 flask-user package > usermixin.py 并更改以下代码。

行前:

user_password = '' if user_manager.USER_ENABLE_AUTH0 or user == None else user.password[-8:]

行后:

user_password = '' if user_manager.USER_ENABLE_AUTH0 else user.password[-8:]