本文介绍了缺少用户加载器或request_loader错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到了烧瓶登录问题,用户加载程序不起作用
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.
和当我尝试在shell中导入用户模型时:
andwhen i try to import the user model in shell:
AttributeError: 'LoginManager' object has no attribute 'load_user'
这是我的代码:
-------------------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
推荐答案
装饰器应该是
@ login_manager.user_loader
不是 load_user
这篇关于缺少用户加载器或request_loader错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!