问题描述
我使用“flask fab create-addon”创建了一个烧瓶插件。
我想更改模板 appbuilder/general/security/login_oauth.html 所以我有:
templates
appbuilder
general
security
login_oauth.html
但是当我加载主机应用程序时,我的 login_oauth.html 版本没有加载。我尝试使用以下代码注册 this post 中的蓝图:
from flask import Blueprint
bp = Blueprint('fab_addon_fslogin',__name__,template_folder='templates')
class MyAddOnManager(BaseManager):
def __init__(self,appbuilder):
"""
Use the constructor to setup any config keys specific for your app.
"""
super(MyAddOnManager,self).__init__(appbuilder)
self.appbuilder.get_app.config.setdefault("MYADDON_KEY","SOME VALUE")
self.appbuilder.register_blueprint(bp)
def register_views(self):
"""
This method is called by AppBuilder when initializing,use it to add you views
"""
pass
def pre_process(self):
pass
def post_process(self):
pass
但是 register_blueprint(bp) 返回:
File "/home/cquiros/data/projects2017/personal/software/superset/addons/fab_addon_fslogin/fab_addon_fslogin/manager.py",line 24,in __init__
self.appbuilder.register_blueprint(bp)
File "/home/cquiros/data/projects2017/personal/software/superset/env_superset/lib/python3.8/site-packages/Flask_AppBuilder-3.3.0-py3.8.egg/flask_appbuilder/base.py",line 643,in register_blueprint
baseview.create_blueprint(
AttributeError: 'Blueprint' object has no attribute 'create_blueprint'
没有太多关于如何做到这一点的信息。任何线索表示赞赏
解决方法
如果您想自定义 login_oauth.html
,最简单的方法是将其直接添加到您的应用中,而不是插件。
这意味着 login_oauth.html
应该放在这个路径中。
YourApp
- app
-- templates
--- appbuilder
---- general
----- security
------ login_oauth.html
对于插件的解决方案,函数 self.appbuilder.register_blueprint
是针对视图而不是针对 Blueprint
函数返回的对象。应该换成
self.appbuilder.get_app.register_blueprint(Blueprint('fab_addon_fslogin',__name__,template_folder='templates'))
保持蓝图注册和
尝试将 login_oauth.html
重命名为 login_oauth_xxxx.html
,其位于
{your python packages root path}\site-packages\flask_appbuilder\templates\appbuilder\general\security
这将使模板根据需要被覆盖。我猜 app-builder 包的模板搜索顺序大于插件。蓝图的搜索顺序取决于注册的顺序
最后,我找到了一个不用重命名文件的技巧,你可以试试下面的方法
class MyAddOnManager(BaseManager):
def __init__(self,appbuilder):
"""
Use the constructor to setup any config keys specific for your app.
"""
super(MyAddOnManager,self).__init__(appbuilder)
self.appbuilder.get_app.config.setdefault('MYADDON_KEY','SOME VALUE')
self.static_bp = Blueprint('fab_addon_fslogin',template_folder='templates')
def register_views(self):
"""
This method is called by AppBuilder when initializing,use it to add you views
"""
pass
def pre_process(self):
self.appbuilder.get_app.register_blueprint(self.static_bp)
blueprint_order = self.appbuilder.get_app._blueprint_order
# move blueprint order of addon to top
blueprint_order.insert(0,blueprint_order.pop())
def post_process(self):
pass