gpt4 book ai didi

python - Flask:多个蓝图相互干扰

转载 作者:太空狗 更新时间:2023-10-30 01:01:49 38 4
gpt4 key购买 nike

我正在用蓝图测试 Flask。我的应用有两个蓝图:

  1. 基地
  2. 意见

基础/__init__.py

base = Blueprint('base', __name__, static_folder='static', template_folder='templates') 
#http://server.com/base

意见/__init__.py

opinions = Blueprint('opinions', __name__, static_folder='static', template_folder='templates')
#http://server.com/opinions

__初始化__.py

app = Flask(__name__)
from app.base import views
from app.base import base
app.register_blueprint(base, url_prefix='/base')

from app.opinions import views
from app.opinions import opinions
#app.register_blueprint(opinions, url_prefix='/opinions') <-- Uncommenting this line causes issues

如果我只注册这些蓝图中的 1 个,则一切正常。但是,如果我注册两个蓝图,模板总是从 opinions 加载。例如,如果我点击 http://server.com/base ,index.html 是从 opinions 文件夹中挑选出来的。 Flask 文档没有提及任何关于“template_folder”命名空间冲突的内容。

PS - 我想知道处理多个蓝图的替代方法。我不太习惯从两个不同的蓝图导入 views 文件。执行此操作的更好方法是什么?

最佳答案

蓝图模板目录是全局注册的。它们共享一个命名空间,以便您的应用程序可以在必要时覆盖蓝图的模板。文档中简要提到了这一点。

因此,您的意见模板不应命名为index.html,而应命名为opinions/index.html。乍一看,这形成了尴尬的路径 (…/opinions/templates/opinions/…),但增加了在不更改蓝图内容的情况下自定义“固定”模板的灵 active 。

关于python - Flask:多个蓝图相互干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22190881/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com