Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flask-login:Exception: No user_loader has been installed for this LoginManager. Add one with the 'LoginManager.user_loader' decorator

I want to use flask_login to manager user login but some error like this:

Exception: No user_loader has been installed for this LoginManager. Add one with the 'LoginManager.user_loader' decorator.

this is my models.py (PS:I use Flask peewee to build my models)

from peewee import *
from playhouse.fields import ManyToManyField
from .__init__ import login_manager
from flask_login import UserMixin, AnonymousUserMixin

db = SqliteDatabase('adminSystem.db')

class BaseModel(Model):
    class Meta:
        database = db

class User(UserMixin, BaseModel):
    name = CharField()
    email = CharField()
    is_admin = BooleanField()
    username = CharField()
    passwd_hash = CharField()
    teacher = ForeignKeyField('self', related_name='students', null=True)

    #def is_authenticated(self):
    #    return True

    #def is_active(self):
    #    return True

    #def is_anonymous(self):
    #    return False

    #def get_id(self):
    #    try:
    #        return unicode(self.id)
    #   except NameError:
    #        return str(self.id)

class Homework(BaseModel):
    owner = ForeignKeyField(User, related_name='homeworks', null=True)
    pub_datetime = DateTimeField()
    connect = TextField()
    homework_id =CharField()
    file_url = CharField()

class Score(BaseModel):
    homework_id = CharField()
    stu_id = CharField()
    grade = IntegerField()
    file_url = CharField()

class AnonymousUser(AnonymousUserMixin):
    def can(self, permissions):
        return False

    def is_administrator(self):
        return False

@login_manager.user_loader
def load_user(userid):
    try:
        #: Flask Peewee used here to return the user object
        return User.get(User.id==userid)
    except User.DoesNotExist:
        return None

and this is some part of my view.py

from flask import render_template, url_for, redirect, request, g
from . import auth
from werkzeug.security import generate_password_hash, check_password_hash
from ..models import db, User, Homework, Score
from flask_login import login_user, logout_user, login_required, current_user

@auth.before_app_request
def before_request():
    g.user = current_user

@auth.route('/login', methods=['POST', 'GET'])
def login():
    if request.method == 'POST':
        if request.form['role'] == 'teacher':
            user = User.get(User.username == request.form['username'])
            if check_password_hash(user.passwd_hash, request.form['password']):
                login_user(user)
                return redirect(url_for('main.post'))
            return render_template('login.html')
        elif request.form['role'] == 'student':
            user = User.get(User.username == request.form['username'])
            if check_password_hash(user.passwd_hash, request.form['password']):
                login_user(user)
                return redirect(url_for('main.show'))
            return render_template('login.html')
    return render_template('login.html')

and there is a picture that I had try

enter image description here

like image 225
Shawn Lu Avatar asked Oct 07 '16 02:10

Shawn Lu


People also ask

What is LoginManager in flask?

from flask_login import LoginManager login_manager = LoginManager() The login manager contains the code that lets your application and Flask-Login work together, such as how to load a user from an ID, where to send users when they need to log in, and the like. login_manager.

How can you tell if someone is logged into a flask?

At the same time, you can use Flask-login API to do some configurations in case that you want to use its functionality. When you want to check whether the user has logged in manually rather than use the Flask-login API, then check the value of session['logged_in'] .


1 Answers

you can place this method

@login_manager.user_loader
def load_user(user_id):
    return None

at after login_manager.init_app(app)

like image 101
code Avatar answered Oct 15 '22 10:10

code