Skip to main content

Flask-Login with multiple models

Project description

flask_login_multi

Flask-Login with multiple models: User Model,Admin Model ...

#####1.setup Flask_Login #####2.import flask_login_multi

from flask import Flask
from flask_login_multi.login_manager import LoginManager   

app=Flask(__name__)

login_manager = LoginManager(app)   

login_manager.blueprint_login_views = {  
        'user':  "user.user_login",  
        'admin': "admin.admin_login",  
    }  

#####3.set buleprint

admin_app = Blueprint('admin', __name__, url_prefix="/admin")  
user_app = Blueprint('user', __name__, url_prefix="/user")  

#####4. model add user_loader

@login_manager.user_loader
def load_user(id, endpoint='user'):
    if endpoint == 'admin':
        return Admin.query.get(id)
    else:
        return User.query.get(id)

#####5. admin or user login

 from app.libs.flask_login_multi import login_user
 #user
 user = User.query.filter_by(name=form.name.data).first()
 login_user(user,remember=True)
 #admin
 user = Admin.query.filter_by(name=form.name.data).first()
 login_user(admin)

#####6.admin login required

from app.libs.flask_login_multi import login_required,current_user  

@admin_app.route('/index')  
@login_required  
def index():  
   print(current_user)  
   return 'admin.index'  

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
Flask_Login_Multi-0.1.2-py2.py3-none-any.whl (5.5 kB) Copy SHA256 hash SHA256 Wheel py2.py3

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page