首页 > 解决方案 > 烧瓶插件如何覆盖模板?

问题描述

我使用“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'

没有太多关于如何做到这一点的信息。任何线索表示赞赏

标签: pythonflaskflask-appbuilder

解决方案


如果您想自定义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.htmllogin_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

参考: flask-appbuilder 定制


推荐阅读