Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing userloader or request_loader error

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
like image 353
B. Fateh Avatar asked May 01 '20 11:05

B. Fateh


1 Answers

The decorator should be

@login_manager.user_loader not load_user

like image 105
mowienay Avatar answered Oct 18 '22 10:10

mowienay