如何将flask-admin添加到蓝图?

例如:如何将flask-admin添加到蓝图?

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()

,但是,如果我需要一个新的文件,名为“views.py”,我怎么能添加视图到views.py来管理? 我需要使用蓝图吗?

回答:

你不需要蓝图。在views.py添加对admin对象,你在你的主要项目中定义的导入:

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'))

,并在主projectmodule文件使用:

from flask import Flask 

from flask.ext.admin import Admin

app = Flask(__name__)

admin = Admin(app)

# import the views

import views

app.run()

例如你在后面加上import views设定的线admin = Admin(app)

回答:

我对这个问题已经很晚了,但无论如何...我的猜测是你想要使用应用程序工厂模式并使用Flask-Admin。有关于潜在问题的nice discussion。我用了一个很丑陋的解决方案,在初始化 .py文件实例化瓶 - 管理:

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

回答:

对于项目中,我实际上是由一个子类蓝图的支持烧瓶管理:

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):

print app

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)

欲了解详细信息,请点击此处阅读我的博客:http://www.sadafnoor.com/blog/how-to-add-flask-admin-to-a-blueprint/

以上是 如何将flask-admin添加到蓝图? 的全部内容, 来源链接: utcz.com/qa/266455.html

回到顶部