9

i have an isssue with flask-login the user loader is not working


Exception: Missing user_loader or request_loader. Refer to http://flask-login.readthedocs.io/#how-it-works for more info.

and when i try to import the user model in shell:

AttributeError: 'LoginManager' object has no attribute 'load_user'

This is my code:

-------------------app/__init__.py----------------

from app.config import Config
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager


#create app
app = Flask(__name__)
app.config.from_object(Config)

#init pckages
db = SQLAlchemy(app)
login_manager = LoginManager(app)

#import Blueprints
from app.blueprints.user.routes import user
from app.blueprints.dashboard.routes import dashboard

#register Blueprints
app.register_blueprint(user)
app.register_blueprint(dashboard)

------------------------------app.py-------------------------------
from app import app

if __name__ == "__main__":

    app.run(debug=True)
------------------------------user model------------------------------------
from app import db, login_manager
from flask_login import UserMixin


@login_manager.load_user
def load_user(user):
    return User.get(user)


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(20), unique=True, nullable=False)
    avatar = db.Column(db.String(50), default="default.jpg")
    password = db.Column(db.String(60), nullable=False)


    def __repr__(self):
        return self.username
Alireza HI
  • 1,873
  • 1
  • 12
  • 20
B. Fateh
  • 227
  • 1
  • 5
  • 12

3 Answers3

16

The decorator should be

@login_manager.user_loader not load_user

mowienay
  • 1,264
  • 4
  • 19
  • 32
  • Still I am getting the same error. can you please check this here : https://stackoverflow.com/questions/65589955/attributeerror-module-flask-login-login-manager-has-no-attribute-user-loader – Happy Coder Jan 06 '21 at 03:57
1

Instead of :

@login_manager.load_user
def load_user(user):
    return User.get(user)

try :

@login_manager.user_loader
def load_user(user):
    return User.query.get(int(user))
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
K4C
  • 11
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 06 '22 at 14:14
  • While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jul 19 '22 at 07:15
0

You can make a slight modification to your current user model. As it is currently, you have:

@login_manager.load_user
def load_user(user):
    return User.get(user)

Consider this:

  1. Import LoginManager from flask_login
from flask_login import LoginManager

login = LoginManager(app)
  1. Update your user model as follows:
from flask_login import UserMixin
from app import login # the variable from Flask-login

@login.user_loader
def load_user(id):
    return User.query.get(int(id)) # slightly modified such that the user is loaded based on the id in the db

The user loader is registered with Flask-Login with the @login.user_loader decorator. The id that Flask-Login passes to the function as an argument is going to be a string, so databases that use numeric IDs need to convert the string to integer as you see above.

Gitau Harrison
  • 3,179
  • 1
  • 19
  • 23