How do I add flask-admin to a Blueprint?

℡╲_俬逩灬. 提交于 2019-12-04 09:57:59

问题


for example:

from flask import Flask
from flask.ext.admin import Admin, BaseView, expose

class MyView(BaseView):
    @expose('/')
    def index(self):
        return self.render('index.html')

app = Flask(__name__)

admin = Admin(app)
admin.add_view(MyView(name='Hello'))

app.run()

but, if I need a new file, called 'views.py', how can I add a view into views.py to admin? Do I need to use a blueprint?


回答1:


For my project I actually made a child class of Blueprint that supports flask admin:

from flask import Blueprint
from flask_admin.contrib.sqla import ModelView
from flask_admin import Admin

class AdminBlueprint(Blueprint):
    views=None


    def __init__(self,*args, **kargs):
        self.views = []
        return super(AdminBlueprint, self).__init__('admin2', __name__,url_prefix='/admin2',static_folder='static', static_url_path='/static/admin')


    def add_view(self, view):
        self.views.append(view)

    def register(self,app, options, first_registration=False):
        admin = Admin(app, name='microblog', template_mode='adminlte')

        for v in self.views:
            admin.add_view(v)

        return super(AdminBlueprint, self).register(app, options, first_registration)

For details you may like to read my blog here: http://blog.sadafnoor.me/blog/how-to-add-flask-admin-to-a-blueprint/




回答2:


You don't need a blueprint for that. In views.py add an import for the admin object you defined in your main project:

from projectmodule import admin
from flask.ext.admin import BaseView, expose

class MyView(BaseView):
    @expose('/')
    def index(self):
        return self.render('index.html')

admin.add_view(MyView(name='Hello'))

and in your main projectmodule file use:

from flask import Flask
from flask.ext.admin import Admin


app = Flask(__name__)
admin = Admin(app)

# import the views
import views

app.run()

e.g. you add import views after the line that sets admin = Admin(app).




回答3:


I am very late for this question, but anyway... My guess is that you want to use the Application Factory pattern and use the Flask-Admin. There is a nice discussion about the underlying problems. I used a very ugly solution, instantiating the Flask-Admin in the init.py file:

from flask_admin.contrib.sqla import ModelView

class UserModelView(ModelView):

    create_modal = True
    edit_modal = True
    can_export = True

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config[config_name])
    db.init_app(app)


    # import models here because otherwise it will throw errors    
    from models import User, Sector, Article

    admin.init_app(app)
    admin.add_view(UserModelView(User, db.session))      

    # attach blueprints
    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint, url_prefix='/auth')

    return app


来源:https://stackoverflow.com/questions/22064871/how-do-i-add-flask-admin-to-a-blueprint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!