Django:如果已覆盖管理模板,则无法覆盖它们? [英] Django: Can't override admin templates when they are already overridden?
问题描述
要向模型的Django管理视图中添加一些内容,我想覆盖 change_form.html
模板。根据文档,我需要在文件夹 / project-path / templates / admin / appname / modelname /
中创建文件 change_form.html
。当然,我需要确保在 TEMPLATE_DIRS
中也可以使用此路径。这样的文件可能看起来像这样:
To add some content to a Django admin view of a model, I want to override the change_form.html
template. According to the documentation I would need to create a file change_form.html
in a the folder /project-path/templates/admin/appname/modelname/
. Of course I need to make sure, that this path is also available in TEMPLATE_DIRS
. Such a file could look like this:
{% extends "admin/change_form.html" %}
{% load i18n %}
{% block after_field_sets %}
SOME CONTENT
{% endblock %}
但是,我使用了 django-guardian 具有对象权限。这个Django应用程式也会覆写 change_form.html
(运作良好-相关来源似乎是此处),但Django不会选择我的模板扩展文件(即,上述示例中的 SOME CONTENT未显示) )。我要覆盖的块/部分 change_form.html
中添加其他内容django-guardion 和我的模板。
However, I make use of django-guardian to have object permissions. This Django app overrides change_form.html
as well (which works fine -- relevant source seems to be here), but Django doesn't pick up my template extension file (i.e. "SOME CONTENT" from the sample above isn't displayed). The blocks/parts I want to override are not the same ones that django-guardian overrides and eventually I want to have the additions to change_form.html
of django-guardion and of my template.
我在这里做什么错了?
What am I doing wrong here? And is possible at all to have multiple applications overriding an admin template?
如果有兴趣,那么这就是我的 TEMPLATE_LOADERS
设置:
If it is of interest, this is my TEMPLATE_LOADERS
setting:
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader'
)
另外,django- Guardian是 INSTALLED_APPS
数组中的最后一个应用。
Also, django-guardian is the last app in the INSTALLED_APPS
array.
推荐答案
一个解决方案似乎是通过引用并覆盖django-guardian的模板(定义此处),而不是Django的常规 change_form.html
。因此,不要使用
One possible solution seems to be to explicitly define the inheritance chain by referring to and overriding django-guardian's template (defined here) and not Django's general change_form.html
. So instead of using
{% extends "admin/change_form.html" %}
在自定义模板的开头,我需要使用
in the beginning of my custom template, I would need to use
{% extends "admin/guardian/model/change_form.html" %}
此外,我还需要扩展 GuardedModelAdmin
子类模型,以明确使用我自己的模板文件作为更改表单模板:
Also I would need to extend my GuardedModelAdmin
sub-class model to explicitly use my own template file as the change form template:
class MyModel(GuardedModelAdmin):
change_form_template = 'admin/appname/mymodel/change_form.html'
这是可行的,但是它为模板和模型增加了明显的依赖性。当然,该模型仍然具有这种依赖性,但是如果还有一种解决方案仅涉及默认的 change_form.html
,我将很感兴趣-但是,我怀疑这真的不可能。
This works, but it adds a clear dependency to the template and the model. Of course, the model has this dependency anyway, but I would be interested if there is also a solution that refers only to the default change_form.html
-- however, I suspect that this is not really possible.
这篇关于Django:如果已覆盖管理模板,则无法覆盖它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!