如何从Flask extension加载模板

2024-10-01 04:50:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在开发一个烧瓶延伸器。我的扩展部分也是模板。默认情况下,我希望使用FlaskExtension中的模板,除非用户在主flask应用程序中重写它们。问题是默认的模板路径指向main\u flask_app/templates。怎么检查?谢谢。在


Tags: 用户路径模板app应用程序flask烧瓶main
1条回答
网友
1楼 · 发布于 2024-10-01 04:50:29

应用程序的布局以及单独的扩展应该如下所示:

myapp_project/
    myapp/
        __init__.py
        models.py
        ...
        static/
        templates/
            index.html
            ...
            myext/
                mypage.html  # overrides default from ext

myext_project/
    myext/
        __init__.py
        ...
        templates/
            myext/
                mypage.html  # the default template from ext
            ...

注意目录结构是如何相同的。向应用程序添加具有相同路径的模板将覆盖扩展中该路径上的默认值。在

您的扩展将通过在应用程序中注册蓝图来使这些模板可用。蓝图应该设置为使用templates文件夹。在

^{pr2}$

相关问题 更多 >