gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 15:41:48 24 4
gpt4 key购买 nike

例如:

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”的新文件,我如何将 View 添加到 views.py管理?
我需要使用蓝图吗?

最佳答案

对于我的项目,我实际上创建了一个支持 flask 管理的 Blueprint 子类:

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)

有关详细信息,您可以在这里阅读我的博客: http://blog.sadafnoor.me/blog/how-to-add-flask-admin-to-a-blueprint/

关于flask - 如何将 flask-admin 添加到蓝图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22064871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com