4

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

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?

If it is of interest, this is my TEMPLATE_LOADERS setting:

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader'
)

Also, django-guardian is the last app in the INSTALLED_APPS array.

maazza
  • 7,016
  • 15
  • 63
  • 96
tomka
  • 1,275
  • 1
  • 11
  • 17

2 Answers2

5

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.

tomka
  • 1,275
  • 1
  • 11
  • 17
3

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.

Meehow
  • 976
  • 9
  • 15
  • the question is no about overriding a template but about overriding them twice when you are using a third party app – maazza Sep 13 '13 at 08:52
  • Django will not merge templates automatically and basically you can't override templates twice, but you can add `{% include "admin/guardian/model/change_form.html" %}` in your templates and use only yours. – Meehow Sep 13 '13 at 09:19
  • Regarding your second point, the documentation and my experience is that from within the INSTALLED_APPS the *first* template Django finds will be used. So if you are trying to override admin/base_site.html from an app, your app better come first. See https://docs.djangoproject.com/en/1.7/ref/templates/api/#django.template.loaders.app_directories.Loader – mlncn Mar 05 '15 at 05:28
  • @mlncn I wrote it in 2013 and it was valid for Django 1.5. I don't know what's the current status. – Meehow Mar 19 '15 at 21:33
  • It changed again in Django 1.11. Now you need to use setting `FORM_RENDERER = 'django.forms.renderers.TemplatesSetting'` https://docs.djangoproject.com/en/1.11/ref/forms/renderers/ – Meehow Sep 07 '17 at 10:53