Flask蓝图中的render_template使用其他蓝图的模板 [英] render_template from Flask blueprint uses other blueprint's template

查看:375
本文介绍了Flask蓝图中的render_template使用其他蓝图的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有蓝图的Flask应用.每个蓝图都提供一些模板.当我尝试从第二个蓝图呈现index.html模板时,将呈现第一个蓝图的模板.为什么blueprint2覆盖blueprint1的模板?如何呈现每个蓝图的模板?

I have a Flask app with blueprints. Each blueprint provides some templates. When I try to render the index.html template from the second blueprint, the first blueprint's template is rendered instead. Why is blueprint2 overriding blueprint1's templates? How can I render each blueprint's templates?

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的模板.

If I change the order the blueprints are registered, then blueprint2's templates override blueprint1's.

application.register_blueprint(bp2)
application.register_blueprint(bp1)

推荐答案

此功能完全按预期工作,尽管没有达到您的预期.

This is working exactly as intended, although not as you expect.

为蓝图定义模板文件夹只会将文件夹添加到模板搜索路径. 不是并不意味着从蓝图的视图中调用render_template只会检查该文件夹.

Defining a template folder for a blueprint only adds the folder to the template search path. It does not mean that a call to render_template from a blueprint's view will only check that folder.

首先在应用程序级别查找模板,然后按照注册蓝图的顺序进行查找.这样一来,扩展程序就可以提供可被应用程序覆盖的模板.

Templates are looked up first at the application level, then in the order blueprints were registered. This is so that an extension can provide templates that can be overridden by an application.

解决方案是在模板文件夹 中使用单独的文件夹,用于与特定蓝图相关的模板.仍然可以覆盖它们,但是要意外地做到这一点要困难得多.

The solution is to use separate folders within the template folder for templates related to specific blueprints. It's still possible to override them, but much harder to do so accidentally.

app/
    blueprint1/
        templates/
            blueprint1/
                index.html
    blueprint2/
        templates/
            blueprint2/
                index.html

将每个蓝图指向其templates文件夹.

Point each blueprint at its templates folder.

bp = Blueprint('bp1', __name__, template_folder='templates')

渲染时,指向templates文件夹下的特定模板.

When rendering, point at the specific template under the templates folder.

render_template('blueprint1/index.html')

有关更多讨论,请参见烧瓶问题#1361 .

See Flask issue #1361 for more discussion.

这篇关于Flask蓝图中的render_template使用其他蓝图的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆