Django:扩展还是包含?

发布于 2024-08-23 02:44:05 字数 389 浏览 4 评论 0原文

我和我的朋友正在发生一些小争执。在我当前的 Django 项目中,我创建了一个名为 menu.html 的文件,其中包含一堆配置并格式化为列表的链接。我目前使用以下 Django/Python 代码包含菜单,而不是手动将菜单硬编码到每个页面中:

{% include 'menu.html' %}

但是,我的朋友建议这是错误的方法。他说我需要使用扩展而不是包含,然后定义内容,如下所示:

{% extend 'menu.html' %}
{% block content %}
The rest of my content here.
{% endblock %}

这是一些额外的代码。我使用哪个真的很重要吗?我更愿意使用前者。

My friend and I are having a small argument. In my current Django Project, I have created a file called menu.html which will contain a bunch of links configured and formatted into a list. Instead of manually hard-coding the menu into each page, I am currently including the menu using the following Django/Python code:

{% include 'menu.html' %}

However, my friend is suggesting that this is the incorrect way to do it. He said I need to use extends instead of include and then define content, something like this:

{% extend 'menu.html' %}
{% block content %}
The rest of my content here.
{% endblock %}

That's a bit of extra code. Does it really matter which I use? I would prefer to use the former.

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

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

发布评论

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

评论(3

尾戒 2024-08-30 02:44:07

是的,这很重要。首先,extends 只能出现在文件的第一行。其次,include 在解析堆栈上推送和弹出一个上下文对象,这意味着在包含时在上下文中创建的值在返回时将超出范围。

我的规则是:创建 base.html 模板文件来定义站点的整体结构,并在关键区域周围使用大量的 {% block foo %} 。然后,所有其他模板都扩展基础(或本身扩展基础的东西),并且您可以根据需要替换这些块。

另一方面,include 适合封装您可能需要在多个地方(甚至可能在同一页面上)使用的内容。

更新:

我一直在使用自己的template_tags库,以至于我忘记了Django的模板语言在功能上仍然存在重大差距。这里有问题的标签来自一个名为 expr 的早期 django 片段,我将其进行了大量编辑和扩展。例如,您可以说 {% expr 'Fred' as name %} (或任何有效的 Python 表达式),它会将结果存储在当前的 'name' 槽中上下文。如果这种情况发生在 included 模板中,则 name 的值将在退出模板文件时弹出。

您可以使用 {% with %} 标记来实现此目的,但 expr 为我提供了更大的灵活性,包括执行任意复杂的调用。这最初是在必须创建复杂的缓存对象时出现的,这些对象需要昂贵的 DBMS 交互,而这些交互无法在视图中完成,必须在模板本身中调用它们。

如果您需要更深入地了解这一点,请给我发电子邮件(在我的个人资料中)。

Yes, it matters. First of all extends can only occur as the very first line of the file. Secondly, include pushes and pops a context object on the resolve stack, which means that value created in the context while in the include will go out of scope when it returns.

My rule is: create base.html template files that define the overall structure of your site and use liberal amounts of {% block foo %} around critical areas. Then all of your other templates extends the base (or something that itself extends the base) and you replace those blocks as needed.

include, on the other hand, is good for encapsulating things you may need to use in more than one place, maybe even on the same page.

Update:

I have been using my own library of template_tags for so long that I forget that Django's template language still has major gaps in functionality. The tag in question here is from an early django snippet called expr which I have heavily edited and extended. You can say, for example, {% expr 'Fred' as name %} (or any valid Python expression), and it will store the result in the 'name' slot in the current Context. If this occurs in an included template, name's value will be popped on exit from the template file.

You can sort of achieve this with the {% with %} tag, but expr gives me much greater flexibility, including doing arbitrarily complex calls. This originally came up when having to create complex cached objects that required expensive DBMS interactions that couldn't be done up in the view, they had to be invoked in the template itself.

Email me (in my profile) if you need to get deeper into this.

姐不稀罕 2024-08-30 02:44:07

(他的朋友)

我实际上的意思是定义一个 base.html 这样你就可以继承一个由几个通用部分一致的基本模板,这个包括 doctype,html 元素定义了 3 个内容块和导航块用于在头部覆盖/插入脚本/链接元素的可选区域。

<!doctype>
<html>
<head>
{%block extrahead %} {%endblock %}
</head>
{%block nav %}
<nav>
  <ul>
    <li>home</li>
  </ul>
</nav>
<div id="content">
{% endblock %}
{%block content %}
{% endblock %}
</div>
</html>

然后您可以定义 homepage.html

{% extends "base.html" %}

{% block content %}
homepage content
{% endblock %}

homepage.html 将具有导航,因为它扩展了 base.html

( his friend )

What I actually meant was defining a base.html so you can inherit a base template consistent of several generic sections, this one includes the doctype, html element defines 3 blocks for content and nav and optional area to override/insert script/link elements in the head.

<!doctype>
<html>
<head>
{%block extrahead %} {%endblock %}
</head>
{%block nav %}
<nav>
  <ul>
    <li>home</li>
  </ul>
</nav>
<div id="content">
{% endblock %}
{%block content %}
{% endblock %}
</div>
</html>

Then you can define homepage.html:

{% extends "base.html" %}

{% block content %}
homepage content
{% endblock %}

homepage.html would then have the navigation because it extends base.html.

行至春深 2024-08-30 02:44:07

在这种情况下,将菜单放在 base.html 中并从中扩展似乎更有意义。

include 非常适合分割复杂的模板并重用这些块。

比方说,您在网站的不同位置使用相同的列表样式,但向其发送其他查询集。只要和你调用查询集一样,你只需要编写一次模板代码。

这里我使用不同的模板进行正常-和 ajax 请求。但是使用 include 让我可以重复使用两个模板中的大部分部分

In this case placing the menu in base.html and extending from this seems to be more senseful.

including is great for splitting complex template and reusing those chunks.

let's say, you use the same list-style in different places of the site, but you send other queryset to it. as long, as you call the querysets the same, you only need to write the template code once.

Here I use differnt templates for normal- and ajax-requests. But using include let me re-use most parts in both templates

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