python-Flask蓝图中的render_template使用其他蓝图的模板
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python-Flask蓝图中的render_template使用其他蓝图的模板,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1479字,纯文字阅读大概需要3分钟。
内容图文
我有一个带有蓝图的Flask应用.每个蓝图都提供一些模板.当我尝试从第二个蓝图呈现index.html模板时,将呈现第一个蓝图的模板.为什么blueprint2覆盖blueprint1的模板?如何呈现每个蓝图的模板?
app/
__init__.py
blueprint1/
__init__.py
views.py
templates/
index.html
blueprint2/
__init__.py
views.py
templates/
index.html
blueprint2 / __ init__.py:
from flask import Blueprint
bp1 = Blueprint('bp1', __name__, template_folder='templates', url_prefix='/bp1')
from . import views
blueprint2 / views.py:
from flask import render_template
from . import bp1
@bp1.route('/')
def index():
return render_template('index.html')
app / __ init__.py:
from flask import Flask
from blueprint1 import bp1
from blueprint2 import bp2
application = Flask(__name__)
application.register_blueprint(bp1)
application.register_blueprint(bp2)
如果我更改了蓝图的注册顺序,那么blueprint2的模板将覆盖blueprint1的模板.
application.register_blueprint(bp2)
application.register_blueprint(bp1)
解决方法:
尽管没有达到您的期望,但它确实按预期工作.
为蓝图定义模板文件夹只会将文件夹添加到模板搜索路径.这并不意味着从蓝图的视图调用render_template只会检查该文件夹.
首先在应用程序级别查找模板,然后按照注册蓝图的顺序进行查找.这样一来,扩展程序就可以提供可被应用程序覆盖的模板.
解决方案是在template文件夹中使用单独的文件夹来存储与特定蓝图相关的模板.仍然可以覆盖它们,但是要意外地覆盖它们则要困难得多.
app/
blueprint1/
templates/
blueprint1/
index.html
blueprint2/
templates/
blueprint2/
index.html
render_template('blueprint1/index.html')
有关更多讨论,请参见Flask issue #1361.
内容总结
以上是互联网集市为您收集整理的python-Flask蓝图中的render_template使用其他蓝图的模板全部内容,希望文章能够帮你解决python-Flask蓝图中的render_template使用其他蓝图的模板所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。