login_user() Nonetype 对象没有属性 is_active 和 flask_login

时间:2021-06-23 23:24:36

标签: flask flask-sqlalchemy flask-login

我正在关注 this tutorial,当我尝试记住注册后登录的用户时,它不起作用并显示 'NoneType' object has no attribute 'is_active' 错误,但它确实注册了用户,所以我不知道发生了什么,它以前工作过,所以我知道问题不在于函数本身。

代码如下:

def signup():
    if request.method == "POST":
        # getting information into backend
        email = request.form.get('email')
        user_name = request.form.get('userName')
        password_first = request.form.get('passwordFirst')
        password_confirm = request.form.get('passwordConfirm')

        # Checking if everything's ok

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

        if user:
            flash("This email is already linked to an account", category="error")
        elif len(email) < 3:
            flash("Email must be at least 3 characters", category="error")
        elif len(user_name) < 2:
            flash("Username must be at least 2 characters", category="error")
        elif password_first != password_confirm:
            flash("Passwords don't match", category="error")
        elif len(password_first) < 8:
            flash("Password must be at least 8 characters long", category="error")

        # adding user to database
        else:
            new_user = User(email=email, user_name=user_name, password=generate_password_hash(password_first, method="sha256"))
            db.session.add(new_user)
            db.session.commit()
            login_user(user, remember=True)
            flash("Account created successfully! :)", category="success")
            return redirect(url_for("views.home"))


    return render_template("signup.html")

1 个答案:

答案 0 :(得分:1)

应该{​​{1}},而是login_user(user, remember=True)