Builtins.AttributeError:'NoneType'对象没有属性'check_password'

时间:2018-12-17 05:28:00

标签: python flask-login

这是我的aap.py文件 如果我提供的电子邮件和密码未注册,那么他们也可以进入/ login页面。在此行中引发错误“如果user.check_password(form.password.data)并且用户不是None:” 任何帮助将不胜感激。

@app.route('/login', methods=['GET', 'POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():

        user = User.query.filter_by(email=form.email.data).first()

        if user.check_password(form.password.data) and user is not None:

            login_user(user)
            flash('Logged in successfully.')

            # If a user was trying to visit a page that requires a login
            # flask saves that URL as 'next'.
            next = request.args.get('next')

            # Checking if that next exists, otherwise we'll go to
            # the welcome page.
            if next == None or not next[0] == '/':
                next = url_for('welcome_user')

            return redirect(next)
    return render_template("login.html", form=form)    enter code here

1 个答案:

答案 0 :(得分:0)

Python编译器从左向右移动,这意味着在仅使用and运算符检查多个条件时,Python编译器首先检查第一个条件,如果为true,则移至第二个条件,以此类推,以获取后续条件。

结果,如果将给定的行更改为如下所示,则代码将按预期开始工作:

if user is not None and user.check_password(form.password.data):