Django:扩展覆盖的模板
我的设置中有两个应用程序。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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为这对于您描述的情况是不可能的,因为这意味着 INSTALLED_APPS 顺序很重要。正如django书中所述:
据我所知,这不是官方文档。不过,这本书的作者是 Adrian Holovaty 和 Jacob Kaplan-Moss(Django 的创建者),所以我会相信他们的话。
但如果你稍微思考一下,你就会明白为什么排序并不是一个好主意:
它仅在特定的简单情况下有帮助。在稍微复杂的情况下,它没有帮助。例如:
app1
、app2
、app3
。app2
和app3
都扩展/覆盖模板app1/a.html
和app1/b.html
。app2
中定义的a.html
和app3
中定义的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:
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.:
app1
,app2
,app3
.app2
andapp3
extend/override templatesapp1/a.html
andapp1/b.html
.a.html
as defined inapp2
andb.html
as defined inapp3
.我认为这是不可能的,除非你有不同的模板名称,然后你可以使用 {{ 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
模板并不真正属于应用程序。如果您愿意,它们可以分组到应用程序目录中,但几乎独立于它们。
覆盖模板的一部分(无论应用程序提供什么)的方法是使用
{% 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.