Django:扩展还是包含?
我和我的朋友正在发生一些小争执。在我当前的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,这很重要。首先,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 templatesextends
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 calledexpr
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 anincluded
template,name
's value will be popped on exit from the template file.You can sort of achieve this with the
{% with %}
tag, butexpr
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.
(他的朋友)
我实际上的意思是定义一个
base.html
这样你就可以继承一个由几个通用部分一致的基本模板,这个包括 doctype,html 元素定义了 3 个内容块和导航块用于在头部覆盖/插入脚本/链接元素的可选区域。然后您可以定义
homepage.html
: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.Then you can define
homepage.html
:homepage.html
would then have the navigation because it extendsbase.html
.在这种情况下,将菜单放在
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