我的烧瓶应用程序布局如下:
myapp/
run.py
admin/
__init__.py
views.py
pages/
index.html
main/
__init__.py
views.py
pages/
index.html_init_.py文件是空的。admin/views.py内容是:
from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='pages')
@admin.route('/')
def index():
return render_template('index.html')to admin/views.py类似于main/views.py
from flask import Blueprint, render_template
main = Blueprint('main', __name__, template_folder='pages')
@main.route('/')
def index():
return render_template('index.html')run.py是:
from flask import Flask
from admin.views import admin
from main.views import main
app = Flask(__name__)
app.register_blueprint(admin, url_prefix='/admin')
app.register_blueprint(main, url_prefix='/main')
print app.url_map
app.run()现在,如果我访问http://127.0.0.1:5000/admin/,它将正确地显示admin/index.html。但是,http://127.0.0.1:5000/main/显示的仍然是admin/index.html,而不是main/index.html。我查看了app.url_map:
<Rule 'admin' (HEAD, OPTIONS, GET) -> admin.index,
<Rule 'main' (HEAD, OPTIONS, GET) -> main.index,此外,我还验证了main/views.py中的index函数是否按预期调用。如果我将main/index.html重命名为不同的名称,那么它可以工作。因此,如果不进行重命名,如何实现1http://127.0.0.1:5000/main/1显示main/index.html?
发布于 2011-11-19 23:56:04
从Flask0.8开始,蓝图将指定的template_folder添加到应用程序的搜索路径中,而不是将每个目录作为单独的实体处理。这意味着如果您有两个具有相同文件名的模板,那么在搜索路径中找到的第一个模板就是使用的模板。诚然,这是令人困惑的,而且此时的文档记录也不充分(请参阅这只虫子)。看起来说你不是唯一一个被这种行为搞糊涂的人。
这种行为的设计原因是因为蓝图模板很容易从主应用程序的模板中被覆盖,这些模板是Flask模板搜索路径中的第一行。
我想到了两个选择。
index.html文件重命名为唯一文件(例如,admin.html和main.html)。yourapp/admin/pages/admin/index.html,然后从蓝图中作为render_template('admin/index.html')调用。https://stackoverflow.com/questions/7974771
复制相似问题