奇怪的模板包含并扩展了 Django 中的行为

发布于 2024-10-02 20:34:11 字数 862 浏览 1 评论 0原文

大师们, 我在这个问题上搜索了很多次,但几乎找不到任何有用的信息。

因此,假设我们有一个 base.html 模板:

{% block test %}This is the base!{% endblock %}

以及其下的 2 个子模板,a.htmlb.html

a.html :

{% extends "base.html" %}
{% block test %}This is the A!{% endblock %}

b.html

{% extends "base.html" %}
{% block test %}This is the B!{% endblock %}

现在我们有第四个模板作为 root.html

<html>
    <body>
        {% include 'a.html' %}
        {% include 'b.html' %}
        {% include 'base.html' %}
    </body>
</html>

因此,当我渲染 root.html 时,我期望得到如下内容:

这是A!这是B!这是基地!

但奇怪的是我得到的总是:

这是A!这是A!这是A!

究竟为什么会发生这种情况?

Gurus,
I googled so many times on this issue but I can barely find any useful information.

So assume that we have a base.html template as:

{% block test %}This is the base!{% endblock %}

And 2 child templates under that, a.html and b.html

a.html:

{% extends "base.html" %}
{% block test %}This is the A!{% endblock %}

b.html

{% extends "base.html" %}
{% block test %}This is the B!{% endblock %}

Now we have 4th template as root.html

<html>
    <body>
        {% include 'a.html' %}
        {% include 'b.html' %}
        {% include 'base.html' %}
    </body>
</html>

So when I render the root.html, I expects to get sth like:

This is the A! This is the B! This is the Base!

But strangely what I got is always:

This is the A! This is the A! This is the A!

Why exactly is this happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-10-09 20:34:11

好吧,我想我找到了根本原因:djang debug_toolbars 插件。
一旦我在 settings.py 中禁用它,那么一切都会正常...

这很奇怪,但我想我会发布插件的问题跟踪列表。

希望这可以帮助遇到同样问题的人

[编辑]
这个bug看起来在最新的0.8.4版本中已经被修复了,而且在0.8.3的时候它一直困扰着我。

OK, I guess I found the root cause: djang debug_toolbars plugin.
Once I disable it in the settings.py then everything works just fine...

This is weird but I guess I would post the issue the plugin's issue tracking list.

Hopefully this could help whoever had the same problem

[EDIT]
This bug looks like being fixed in the latest 0.8.4 revision, and it was bugging me so much in the 0.8.3 time.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文