Flask-login:AttributeError:“用户”对象没有属性“ is_active”

时间:2019-10-24 06:35:07

标签: flask

我正在关注Corey Schafer的教程,并且 每次提交登录详细信息时,我都会不断收到错误消息 AttributeError:“用户”对象没有属性“ is_active”

虽然注册可以正常进行

\\

@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 and bcrypt.check_password_hash(user.password, form.password.data):
            login_user(user, remember=form.remember.data)
            return redirect(url_for('main'))
        else:
            flash('Login Unsuccessful check again')
    return render_template('login.html', title='Login', form=form)

\\

\\

from datetime import datetime
from main import db, login_manager
from flask_login import UserMixin


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))




class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20), nullable=False,
                           default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def is_active(self):
        return True

    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"

\\

1 个答案:

答案 0 :(得分:0)

那是因为您需要在is_active模型中添加列User。例如:

is_active= db.Column(db.Boolean, default=True, nullable=False)