烧瓶蓝图模板文件夹 [英] flask blueprint template folder
问题描述
myapp /
run.py
admin /
__init__.py
views.py
页面/
index.html
main /
__init__.py
views.py
页面/
index.html
_
from flask import Blueprint,render_template
admin = Blueprint('admin',__name__,template_folder ='pages')
@ admin.route('/')
def index():
return render_template('index .html')
main / views.py 类似于 admin / views.py :
from flask import蓝图,render_template
main = Blueprint(' main',__name__,template_folder ='pages')
@ main.route('/')
def index():
return render_template('index.html')
run.py 为:
from flask导入Flask
from admin.views从main.views导入admin
import main
app = Flask (__name__)
app.register_blueprint(admin,url_prefix ='/ admin')
app.register_blueprint(main,url_prefix ='/ main')
打印ap p.url_map
$ b 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中的索引函数是如预期的。
如果我将main / index.html重命名为不同的东西,那么它的工作原理。那么,如果不进行
的重命名,那么如何才能实现那个1http://127.0.0.1:5000/main/1显示main / index.html?
从Flask 0.8开始,蓝图将指定的template_folder添加到应用程序的搜索路径中,而不是将每个目录视为单独的实体。这意味着如果您有两个具有相同文件名的模板,则在搜索路径中找到的第一个模板是使用的模板。这当然是令人困惑的,目前还没有很好的记录(参见这个bug )。 看起来你并不是唯一一个被这种行为困惑的人。
这种行为的设计原因是,蓝图模板可以很容易地从主应用程序模板,这是在Flask的模板搜索路径中的第一个在线。
两个选项让人想起。
index.html
文件重命名为唯一的(例如 admin.html
和 main.html
)。
模板放在blueprint文件夹的一个子目录中,然后使用该子目录调用
模板。例如,您的管理员模板将是 yourapp / admin / pages / admin / index.html
,然后从
中调用蓝图作为 render_template('admin / index.html')
。
My flask app layout is:
myapp/
run.py
admin/
__init__.py
views.py
pages/
index.html
main/
__init__.py
views.py
pages/
index.html
_init_.py files are empty. admin/views.py content is:
from flask import Blueprint, render_template
admin = Blueprint('admin', __name__, template_folder='pages')
@admin.route('/')
def index():
return render_template('index.html')
main/views.py is similar to admin/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 is:
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()
Now, if I access http://127.0.0.1:5000/admin/
, it correctly displays admin/index.html.
However, http://127.0.0.1:5000/main/
shows still admin/index.html instead of main/index.html. I checked app.url_map:
<Rule 'admin' (HEAD, OPTIONS, GET) -> admin.index,
<Rule 'main' (HEAD, OPTIONS, GET) -> main.index,
Also, I verified that index function in main/views.py is called as expected. If I rename main/index.html to something different then it works. So, without renaming, how can achieve that 1http://127.0.0.1:5000/main/1 shows main/index.html?
As of Flask 0.8, blueprints add the specified template_folder to the app's searchpath, rather than treating each of the directories as separate entities. This means that if you have two templates with the same filename, the first one found in the searchpath is the one used. This is admittedly confusing, and is poorly documented at this time (see this bug). It seems that you weren't the only one that was confused by this behavior.
The design reason for this behavior is so that blueprint templates can be easily overriden from the main app's templates, which are first-in-line in Flask's template searchpath.
Two options come to mind.
- Rename each of the
index.html
files to be unique (e.g.admin.html
andmain.html
). - In each of the template folders, put each of the
templates in a subdirectory of the blueprint folder and then call
the template using that subdirectory. Your admin template, for example, would be
yourapp/admin/pages/admin/index.html
, and then called from within the blueprint asrender_template('admin/index.html')
.
这篇关于烧瓶蓝图模板文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!