Flask-login AttributeError: 'User' object

2019-03-13 08:32发布

I have a problem with flask-login. After filling login form and clicking 'Submit' i'm get this error:

Flask-login AttributeError: 'User' object has no attribute 'is_active'

Some test users are created. And no problems with login template

Traceback:

    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

2条回答
ゆ 、 Hurt°
2楼-- · 2019-03-13 09:00

From the Flask-login documentation, it specifically says:

"To make implementing a user class easier, you can inherit from UserMixin, which provides default implementations for all of these methods. (It’s not required, though."

The methods refered will be: is_authenticated(), is_active(), is_anonymous() and get_id(), which by the look of your model, they are missing. Once you implement those functions to your model, there should be no problem with Flask-login.

查看更多
Lonely孤独者°
3楼-- · 2019-03-13 09:05

You should subclass UserMixin on your model. You should also add a 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
查看更多
登录 后发表回答