python - 烧瓶插件如何覆盖模板?
问题描述
我使用“flask fab create-addon”创建了一个烧瓶插件。
我想更改模板 appbuilder/general/security/login_oauth.html 所以我有:
templates
appbuilder
general
security
login_oauth.html
但是当我加载主机应用程序时,我的 login_oauth.html 版本没有加载。我尝试使用以下代码在这篇文章中注册蓝图:
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', __name__, 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
推荐阅读
- spring-boot - Spring 安全性未重定向到给定的 OAuth 身份验证 URL
- python - 我正在尝试将 jwt 合并到我的代码中,但出现模块错误
- scipy - 检查多个数据点是否在 1-sigma 内相互一致
- oracle-nosql - Oracle NoSQL 是否支持 Open JDK?
- php - 如何仅取消设置和销毁一个会话变量
- javascript - 打字稿如何在扩展时添加嵌套类型
- sql - 数据未插入到 SQL Server 表中
- typescript - @apollo/federation 包的替代品
- flutter - 如何保持页面状态在颤动?
- javascript - 在没有 yarn/npm 命令的情况下使用 package.json 脚本