Django: Can't override admin templates when they are already overridden?

元气小坏坏 提交于 2019-12-05 21:37:35

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" %}

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'

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.

If a path to your template is in TEMPLATE_DIRS, then you can reverse order of your TEMPLATE_LOADERS.

If your template in in your app, then you need to add your app after django-guardian in INSTALLED_APPS array.

Basically, if there are name collisions, then last loaded template will be in use.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!