Django:扩展覆盖的模板

发布于 2025-01-03 03:58:25 字数 599 浏览 2 评论 0原文

我的设置中有两个应用程序。INSTALLED_APPS:

INSTALLED_APPS = [
    'application2',
    'application1'
]

并希望 application2 更改 application1 的模板(例如通过添加按钮)。

如何在不覆盖整个模板的情况下实现这一目标?

注意

问题是两个模板具有相同的名称(“mytemplate.html”)和相同的文件夹名称(“application1”):

\project_root
    \application1\templates\application1\mytemplate.html
    \application2\templates\application1\mytemplate.html

所以我无法编写:

{% extends "application1\mytemplate.html" %}

因为两个模板都被命名为“application1” \mytemplate.html”。

I have two applications in my settings.INSTALLED_APPS:

INSTALLED_APPS = [
    'application2',
    'application1'
]

and want application2 to change a template from application1 (e.g. by adding a button).

How can achieve this without overriding the whole template?

NOTE

The problem is that the two templates have the same name ("mytemplate.html") and the same folder name ("application1"):

\project_root
    \application1\templates\application1\mytemplate.html
    \application2\templates\application1\mytemplate.html

so that I cannot write:

{% extends "application1\mytemplate.html" %}

because both templates are named "application1\mytemplate.html".

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

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

发布评论

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

评论(3

谁对谁错谁最难过 2025-01-10 03:58:25

我认为这对于您描述的情况是不可能的,因为这意味着 INSTALLED_APPS 顺序很重要。正如django书中所述

INSTALLED_APPS 的顺序并不重要,但我们喜欢按字母顺序排列,以便于人们阅读。

据我所知,这不是官方文档。不过,这本书的作者是 Adrian Holovaty 和 Jacob Kaplan-Moss(Django 的创建者),所以我会相信他们的话。

但如果你稍微思考一下,你就会明白为什么排序并不是一个好主意:
它仅在特定的简单情况下有帮助。在稍微复杂的情况下,它没有帮助。例如:

  • 您有 app1app2app3
  • app2app3 都扩展/覆盖模板 app1/a.htmlapp1/b.html
  • 您希望使用 app2 中定义的 a.htmlapp3 中定义的 b.html

I don't think this is possible for the case you describe because it implies that INSTALLED_APPS order matters. As it is stated in the django book:

The order of INSTALLED_APPS doesn’t matter, but we like to keep things alphabetical so it’s easy for a human to read.

I understand that this is not the official documentation. However the book is authored by Adrian Holovaty and Jacob Kaplan-Moss (the Django creators), so I'll take their word on it.

But if you think a bit about it you will see why ordering is not such a great idea:
It only helps in specific - easy cases. In slightly more complex cases it wouldn't help. E.g.:

  • You have app1, app2, app3.
  • Both app2 and app3 extend/override templates app1/a.html and app1/b.html.
  • You want to use a.html as defined in app2 and b.html as defined in app3.
私野 2025-01-10 03:58:25

我认为这是不可能的,除非你有不同的模板名称,然后你可以使用 {{ block.super }}

一旦加载程序找到正确的文件,它就不会再查找,所以你无权访问在新模板中覆盖模板。

https://code.djangoproject.com/browser /django/trunk/django/template/loaders/app_directories.py#L57

I don't think it is possible, unless you have different template names, then you can use {{ block.super }}

Once loader finds correct file, it doesn't look any further, so you don't have an access to overridden template in your new template.

https://code.djangoproject.com/browser/django/trunk/django/template/loaders/app_directories.py#L57

辞别 2025-01-10 03:58:25

模板并不真正属于应用程序。如果您愿意,它们可以分组到应用程序目录中,但几乎独立于它们。

覆盖模板的一部分(无论应用程序提供什么)的方法是使用 {% extends 'template_name.html' %} 继承模板,然后定义需要覆盖的任何块。当然,这意味着父模板需要已经定义了这些块 - 否则您需要覆盖已定义的最小相关块,并在需要更改的位周围重复一些内容。

Templates aren't really owned by applications. They can be grouped into application directories if you like, but are pretty much independent of them.

The way to override part of a template, whatever application provided it, is to inherit from it using {% extends 'template_name.html' %} and then define whatever blocks you need to override. Of course, this means that the parent template will need to have those blocks already defined - otherwise you'll need to override the smallest relevant block that is defined, and repeat some of the content around the bit you need to change.

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