1

I have a problem with flask-login. I searched on the site and I have not found the solution...

When I want to connect, I have this message :

AttributeError

AttributeError: 'User' object has no attribute 'is_active'

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request

rv = self.dispatch_request()

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request

return self.view_functions[rule.endpoint](**req.view_args)

File "/home/ya2s/UNIVERSITE/WEB/web-serveur/projetvin/tuto/views.py", line 112, in login

login_user(inscrit)

File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 675, in login_user

if not force and not user.is_active():

AttributeError: 'User' object has no attribute 'is_active'

My code in views :

from wtforms import PasswordField
from .models import User
from hashlib import sha256

class LoginForm(Form):
    username = StringField('Username')
    password = PasswordField('Password')

    def get_authenticated_user(self):
        user = User.query.get(self.username.data)
        if user is None :
            return None
        m = sha256()
        m.update(self.password.data.encode())
        passwd = m.hexdigest()
        return user if passwd == user.password else None

from flask.ext.login import login_user, current_user
from flask import request

@app.route("/login/", methods=("GET","POST",))
def login():
    f = LoginForm()
    if f.validate_on_submit():
        inscrit = f.get_authenticated_user()
        if inscrit is not None : 
            login_user(inscrit)
            return redirect(url_for("home"))
    return render_template(
        "login.html",
        form = f)

My models :

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

class User(db.Model):
    username = db.Column(db.String(50), primary_key=True)
    password = db.Column(db.String(64))

    def get_id(self):
        return self.username

from .app import login_manager

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

3 Answers 3

5

Your User class does not define any method is_active(). Your user class needs to implement these documented methods.

Judging by your imports it looks like you had already intended to do it, but you can use the UserMixin class to provide default implementations for the required User methods. You should just be able to add UserMixin as a base class for your User class:

class User(db.Model, UserMixin):
    username = db.Column(db.String(50), primary_key=True)
    password = db.Column(db.String(64))

    def get_id(self):
        return self.username
Sign up to request clarification or add additional context in comments.

Comments

2

Your User class is missing methods that flask-login expects. Flask-login provides a UserMixin, which you are importing but not using.

Change class User(db.Model): to class User(db.Model, UserMixin):. You can read about the expected methods in the docs.

Comments

1

Here is an example implementation of Flask-Login which helped me resolve all doubts: Flask-admin, please look at line 37, where is_active() is a developer defined function.

Here's snapshot of what I was referring to:

# Create user model.
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(100))
    last_name = db.Column(db.String(100))
    login = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120))
    password = db.Column(db.String(64))

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self): # line 37
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    # Required for administrative interface
    def __unicode__(self):
        return self.username

2 Comments

Link-only answers are bad as the link may not always exist. In this case, you are referencing a line number in the master branch. Changes to the file could result in the contents of the line being different in the future.
took a snapshot over, but really the file in master is a reference for flask-login integration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.