It turns out to log in, but when I send an incorrect login or password, I get an error

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

I'm confused with g.user , which is announced before @ app.route

@app.before_request def before_request(): g.user = current_user 

and the solution does not seem to be correct:

 user = User.query.filter_by(email=form.email.data,password=form.password.data).first() 

models:

 from app import db,unicode,UserMixin ROLE_USER = 0 ROLE_ADMIN = 1 class User(UserMixin,db.Model): id = db.Column(db.Integer, primary_key = True) username = db.Column(db.String(64), index = True, unique = True) email = db.Column(db.String(120), index = True, unique = True) password = db.Column(db.String(120), index = True) role = db.Column(db.SmallInteger, default=ROLE_USER) def __init__(self, id, username, email, password, role): self.id = id self.username = username self.email = email self.password = password self.role = role def get_id(self): return self.id def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def __repr__(self): return 'ID: %r ' % (self.id) + ' USERNAME: %r ' % (self.username) + ' EMAIL: %r ' % (self.email) + ' PASSWORD: %r ' % (self.password) + ' ROLE: %r ' % (self.role) 

views:

 @lm.user_loader def load_user(id): return User.query.get(id) @app.route('/login', methods=('GET', 'POST')) def login(): if g.user is not None and g.user.is_authenticated: return redirect(url_for('index', title=g.user.username)) form = LoginForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data,password=form.password.data).first() print('USER :' + str(user)) print('G.USER :' + str(g.user)) print('USER LOGIN IS : ' + str(login_user(user))) login_user(user) return redirect(url_for('index')) return render_template('login.html', form=form) 

forms:

 class LoginForm(Form): email = StringField('Email', validators=[DataRequired()]) password = PasswordField('New Password', validators=[DataRequired()]) remember_me = BooleanField('Remember me', default=False) 
  • Well, you send in None for login_user None for some reason, and that falls - andreymal
  • In other words, neither WTForms, nor Flask-Login do (and cannot and should not) verify the existence of the user and the correctness of the password, you should do it yourself - andmalmal
  • @andreymal in the documentation in the How it Works section says “It shouldn’t be a return if it’s not valid.” - Narnik Gamarnik
  • @andreymal I apologize for oftop, but why then in the framework all this "magic"? - Narnik Gamarnik
  • one
    The Flask-Login capability is a binding of a user object to a session, that's all. Through login_user you specify which object to bind, Flask-Login retrieves the id from there and saves it in the session, and on subsequent requests to the site it calls load_user to load the object again by id stored in the session. That's it, nothing more is not here. In load_user you can return None. But you can NOT transfer None to login_user . - andreymal

0