【问题标题】:AttributeError: 'NoneType' object has no attribute 'password'AttributeError:“NoneType”对象没有属性“密码”
【发布时间】:2015-12-19 15:50:51
【问题描述】:

我有一个 5 个月前开始的烧瓶项目,并在注册和身份验证后停止。我现在想继续,在全新安装后,我现在收到标题为登录/身份验证的错误。下面

账户表格

class LoginForm(Form):
    email = StringField('Enter email', validators=[DataRequired(),Email()])
    password = PasswordField('Password', validators=[DataRequired()])
    remember = BooleanField('Remember Password')

在路由文件中

@app.route('/',methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
    formLogin = AccountForm.LoginForm()
    if request.method == 'GET' :
        return render_template('index.html',formLogin=formLogin)
    if request.method == 'POST' :
        if request.form.get('login', None)  == 'Login' :
            return AccountController.authenticatePopUpLogin(formLogin,'index')

在我的帐户控制器中

def authenticatePopUpLogin(formLogin,route):
    if formLogin.validate_on_submit():
        try:
            user = session.query(User).filter(User.email == formLogin.email.data).first()
 except :# models.DoesNotExist:
            flash("Your email or password does not match !", "error")
            return render_template('login.html',form=formLogin,formLogin = formLogin)
        else :
            if check_password_hash(user.password,formLogin.password.data):

我的用户是从我的模型类中导入的

class User(UserMixin , Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    title = Column(CHAR(3), nullable = False)
    firstname = Column(String(100), nullable = False)
    lastname = Column(String(100), nullable = False)
    DateOfBirth = Column(ArrowType, default = arrow.utcnow())
    username = Column(String(100), nullable = False, unique = True)
    email = Column (String(50), nullable =False, unique = True)
    password = Column(String(100), nullable = False) 
    ...

然后它会抛出上面的错误。我怀疑错误发生在这里check_password_hash(user.password,formLogin.password.data):。但是,我的表单验证有效,当它为空时会引发错误等。

我确认密码字段也存在于我的数据库中。请问我哪里出错了?

【问题讨论】:

  • 我这一行if check_password_hash(user.password,formLogin.password.data):, user 是小写,而之前是大写。
  • 嗨,User 中的 session.query(User).filter(User.email == formLogin.email.data).first() 来自我的用户类。但是,user 来自变量user(即user = session.query(User).filter(User.email == formLogin.email.data).first()。我认为它们都是小写的先生。

标签: python flask


【解决方案1】:

根据first方法的文档:

返回此查询的第一个结果,如果没有结果,则返回 None 包含任何行。

因此,您必须检查 user 是否为 None

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-01-01
    • 2021-12-26
    • 2019-07-23
    • 2018-05-13
    • 2020-09-07
    • 2017-05-03
    • 2023-03-16
    • 2018-07-14
    相关资源
    最近更新 更多