一尘不染

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

flask

我的flask登录有问题。填写登录表单并单击“提交”后,出现此错误:

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

创建了一些测试用户。而且登录模板没有问题

追溯:

    Traceback (most recent call last):
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\flask_prj\project\venv\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\flask_prj\project\hello.py", line 170, in login
login_user(user, form.remember_me.data)
File "C:\flask_prj\project\venv\lib\site-packages\flask_login.py", line 576, in login_user
if not force and not user.is_active():
AttributeError: 'User' object has no attribute 'is_active'

My code in views:

@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 is not None and user.verify_password(form.password.data):
            login_user(user, form.remember_me.data)
            return redirect(request.args.get('next') or url_for('index'))
        flash('Invalid username or password.')
    return render_template('login.html', form=form)
@login_required
def logout():
    logout_user()
    flash ('You have been logged out')
    return redirect(url_for('index'))

My model:

class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.Integer, unique=True, index=True)
    email = db.Column(db.String(64), unique=True, index=True)
    password_hash = db.Column(db.String(128))
    @property
    def password(self):
        raise AttributeError('password is not a readable attribute')
    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password)
    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    def __repr__(self):
        return '<User %r>' % self.username

阅读 680

收藏
2020-04-07

共1个答案

一尘不染

你应该UserMixin在模型上进行子类化。你还应该添加一个user_loader

from flask.ext.login import UserMixin
from yourapp import login_manager

@login_manager.user_loader
def get_user(ident):
  return User.query.get(int(ident))


class User(db.Model, UserMixin):
  id = db.Column(db.Integer, primary_key=True)
  ### yada yada, you know what goes here
2020-04-07