Flask Addon 如何覆盖模板?

How can a flask Addon overwrite a template?

我使用“flask fab create-addon”创建了一个 flask 插件。

我想更改模板 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) return:

  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 函数 returns 的对象。它应该被替换为

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

参考: flask-appbuilder Customizing