“{%扩展%}”和“{%包括%}”在 Django 模板中

发布于 2024-08-04 00:35:41 字数 508 浏览 2 评论 0原文

我想在两个不同的基本文件中提供相同的内容。

所以,我试图这样做:

page1.html:

{% extends "base1.html" %}
{% include "commondata.html" %}

page2.html:

{% extends "base2.html" %} 
{% include "commondata.html" %}

问题是我似乎无法同时使用扩展和包含。有什么办法可以做到这一点吗?如果没有,我怎样才能实现上述目标?

commondata.html 覆盖 base1.htmlbase2.html 中指定的块。

这样做的目的是在pdfhtml 格式,格式略有不同。上面的问题简化了我想要做的事情,所以如果我能得到答案,它将解决我的问题。

I would like to provide the same content inside 2 different base files.

So, I'm trying to do this:

page1.html:

{% extends "base1.html" %}
{% include "commondata.html" %}

page2.html:

{% extends "base2.html" %} 
{% include "commondata.html" %}

The problem is that I can't seem to use both extends and include. Is there some way to do that? And if not, how can I accomplish the above?

commondata.html overrides a block that is specified in both base1.html and base2.html

The purpose of this is to provide the same page in both pdf and html format, where the formatting is slightly different. The above question though simplifies what I'm trying to do so if I can get an answer to that it will solve my problem.

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

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

发布评论

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

评论(8

撑一把青伞 2024-08-11 00:35:41

当您使用扩展模板标签时,您是在说当前模板扩展了另一个模板——它是一个子模板,依赖于父模板。 Django 将查看您的子模板并使用其内容来填充父模板。

您想要在子模板中使用的所有内容都应该在块内,Django 使用这些块来填充父模板。如果您想在该子模板中使用 include 语句,则必须将其放在一个块中,以便 Django 能够理解它。否则它就没有意义,Django 也不知道如何处理它。

Django 文档有一些使用块替换父模板中的块的非常好的示例。

https://docs.djangoproject.com/en/dev/参考/模板/语言/#template-inheritance

When you use the extends template tag, you're saying that the current template extends another -- that it is a child template, dependent on a parent template. Django will look at your child template and use its content to populate the parent.

Everything that you want to use in a child template should be within blocks, which Django uses to populate the parent. If you want use an include statement in that child template, you have to put it within a block, for Django to make sense of it. Otherwise it just doesn't make sense and Django doesn't know what to do with it.

The Django documentation has a few really good examples of using blocks to replace blocks in the parent template.

https://docs.djangoproject.com/en/dev/ref/templates/language/#template-inheritance

一片旧的回忆 2024-08-11 00:35:41

来自 Django 文档:

include 标签应被视为“呈现此子模板并包含 HTML”的实现,而不是“解析此子模板并包含其内容,就好像它是父模板的一部分一样”。这意味着包含的模板之间没有共享状态——每个包含都是一个完全独立的渲染过程。

因此 Django 不会从 commondata.html 中获取任何块,并且它不知道如何处理块之外渲染的 html。

From Django docs:

The include tag should be considered as an implementation of "render this subtemplate and include the HTML", not as "parse this subtemplate and include its contents as if it were part of the parent". This means that there is no shared state between included templates -- each include is a completely independent rendering process.

So Django doesn't grab any blocks from your commondata.html and it doesn't know what to do with rendered html outside blocks.

⒈起吃苦の倖褔 2024-08-11 00:35:41

这应该可以解决您的问题:将 include 标记放在块部分内。

页面1.html:

{% extends "base1.html" %}

{% block foo %}
   {% include "commondata.html" %}
{% endblock %}

页面2.html:

{% extends "base2.html" %}

{% block bar %}
   {% include "commondata.html" %}
{% endblock %}

This should do the trick for you: put include tag inside of a block section.

page1.html:

{% extends "base1.html" %}

{% block foo %}
   {% include "commondata.html" %}
{% endblock %}

page2.html:

{% extends "base2.html" %}

{% block bar %}
   {% include "commondata.html" %}
{% endblock %}
一人独醉 2024-08-11 00:35:41

有关它为什么对我不起作用的更多信息,以防它对未来的人有帮助:

它不起作用的原因是 django 中的 {% include %} 不喜欢像花哨的撇号这样的特殊字符。我试图包含的模板数据是从word粘贴的。我必须手动删除所有这些特殊字符,然后它才能成功包含。

More info about why it wasn't working for me in case it helps future people:

The reason why it wasn't working is that {% include %} in django doesn't like special characters like fancy apostrophe. The template data I was trying to include was pasted from word. I had to manually remove all of these special characters and then it included successfully.

小霸王臭丫头 2024-08-11 00:35:41

添加以供将来通过 google 找到此内容的人参考:对于此类情况,您可能需要查看夹层库提供的 {% overextend %} 标记。

Added for reference to future people who find this via google: You might want to look at the {% overextend %} tag provided by the mezzanine library for cases like this.

一人独醉 2024-08-11 00:35:41

您无法将包含文件中的块拉入子模板中以覆盖父模板的块。但是,您可以在变量中指定父级,并在上下文中指定基本模板。

来自文档

{%extendsvariable%} 使用变量的值。如果变量的计算结果为字符串,Django 将使用该字符串作为父模板的名称。如果变量计算结果为 Template 对象,Django 将使用该对象作为父模板。

不要将“page1.html”和“page2.html”分开,而是将 {% extends base_template %} 放在“commondata.html”的顶部。然后在您的视图中,将 base_template 定义为“base1.html”或“base2.html”。

You can't pull in blocks from an included file into a child template to override the parent template's blocks. However, you can specify a parent in a variable and have the base template specified in the context.

From the documentation:

{% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.

Instead of separate "page1.html" and "page2.html", put {% extends base_template %} at the top of "commondata.html". And then in your view, define base_template to be either "base1.html" or "base2.html".

冷︶言冷语的世界 2024-08-11 00:35:41

2015 年 12 月 10 日编辑:正如评论中指出的,ssi 自 1.8 版以来已被弃用。根据文档:

此标签已被弃用,并将在 Django 1.10 中删除。请改用 include 标签。


在我看来,这个问题的正确(最佳)答案是来自 podshumok 的答案,因为它解释了为什么 include 与继承一起使用时的行为。

然而,令我有些惊讶的是,没有人提到 Django 模板系统提供的 ssi 标签,该标签是专门为 内联 设计的,包括外部的文本嗯>。这里,内联意味着外部文本不会被解释、解析或插入,而只是在调用模板内“复制”。

请参阅文档以获取更多详细信息(请务必在页面右下部分的选择器中检查相应的 Django 版本)。

https://docs.djangoproject.com/en/dev/ref /templates/builtins/#ssi

来自文档:

<前><代码>ssi
将给定文件的内容输出到页面中。
与简单的 include 标记一样,{% ssi %} 包含另一个文件的内容
– 必须使用绝对路径指定 – 在当前页面中

还要注意此技术的安全隐患以及所需的 ALLOWED_INCLUDE_ROOTS 定义,必须将其添加到您的设置文件中。

Edit 10th Dec 2015: As pointed out in the comments, ssi is deprecated since version 1.8. According to the documentation:

This tag has been deprecated and will be removed in Django 1.10. Use the include tag instead.


In my opinion, the right (best) answer to this question is the one from podshumok, as it explains why the behaviour of include when used along with inheritance.

However, I was somewhat surprised that nobody mentioned the ssi tag provided by the Django templating system, which is specifically designed for inline including an external piece of text. Here, inline means the external text will not be interpreted, parsed or interpolated, but simply "copied" inside the calling template.

Please, refer to the documentation for further details (be sure to check your appropriate version of Django in the selector at the lower right part of the page).

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#ssi

From the documentation:

ssi
Outputs the contents of a given file into the page.
Like a simple include tag, {% ssi %} includes the contents of another file
– which must be specified using an absolute path – in the current page

Beware also of the security implications of this technique and also of the required ALLOWED_INCLUDE_ROOTS define, which must be added to your settings files.

浅唱々樱花落 2024-08-11 00:35:41

您可以同时使用 {% extends %} 和 < a href="https://docs.djangoproject.com/en/4.2/ref/templates/builtins/#include" rel="nofollow noreferrer">{% include %} 在一个模板中。

例如,下面有base.html,显示Base

# "templates/base.html"

<p>Base</p>

{% block content %}{% endblock %}

并且,下面有additional.html,显示Additional:

# "templates/additional.html"

<p>Additional</p>

并且,下面有 index.html,其中显示 Index

# "templates/index.html"

<p>Index</p>

现在,您可以扩展 base.html 并包含 其他内容.html 位于 {% 块内%} 除了 index.html 中的

Index

如下所示,然后渲染 index.html 与 Django 视图:

# "templates/index.html"

{% extends "base.html" %}

{% block content %}
    <p>Index</p>
    {% include "additional.html" %}
{% endblock %}

然后,BaseIndexAdditional 显示如下:

Base

Index

Additional

注意,{% block %} 下面和代码{% block %} 内部 base.html 中没有的内容在 index.html 中被忽略:

# "templates/index.html"

{% extends "base.html" %}

<p>Index</p>
{% include "additional.html" %}

{% block test %}
    <p>Index</p>
    {% include "additional.html" %}
{% endblock %}

因此,只有 Base显示如下:

Base

You can use both {% extends %} and {% include %} in one template.

For example, there is base.html below which displays Base:

# "templates/base.html"

<p>Base</p>

{% block content %}{% endblock %}

And, there is additional.html below which displays Additional:

# "templates/additional.html"

<p>Additional</p>

And, there is index.html below which displays Index:

# "templates/index.html"

<p>Index</p>

Now, you can extend base.html and include additional.html inside of {% block %} in addition to <p>Index</p> in index.html as shown below, then render index.html with a Django View:

# "templates/index.html"

{% extends "base.html" %}

{% block content %}
    <p>Index</p>
    {% include "additional.html" %}
{% endblock %}

Then, Base, Index and Additional are displayed as shown below:

Base

Index

Additional

Be careful, the code outside of {% block %} below and the code inside of {% block %} which base.html doesn't have below are ignored in index.html:

# "templates/index.html"

{% extends "base.html" %}

<p>Index</p>
{% include "additional.html" %}

{% block test %}
    <p>Index</p>
    {% include "additional.html" %}
{% endblock %}

So, only Base is displayed as shown below:

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