Django: Overriding AND extending an app template

As an update since this seems to be a popular question. I have used the overextends app without any problem. It provides a new keywords overextends that allows to extend templates with the same name.

And it is easy to install with pip:

 pip install -U django-overextends

Django 1.9 and later has this feature:

Django template loaders can now extend templates recursively.

So you can have a file from app1 with contents like this:

app1/templates/example/foobar.html:

<p>I'm from app1</p>
{% block main %}{% endblock %}

And you can extend it with a file from app2 (notice that the template name example/foobar.html is the same):

app2/templates/example/foobar.html:

{% extends "example/foobar.html" %}
{% block main %}
  <p>I'm from app2</p>
{% endblock %}

The end-result should be:

<p>I'm from app1</p>
<p>I'm from app2</p>

I think the answer from this related question is pertinent; currently, the best solution seems to be to use a custom template loader from the django-apptemplates package on PyPI, so you can just use pip to install it (e.g. pip install django-apptemplates).

The template loader allows you to extend a template in a specific app; for example, to extend the index page of the admin inteface, you would add

'apptemplates.Loader',

to your TEMPLATE_LOADERS list in settings.py, and use

{% extends "admin:admin/index.html" %}

in your templates.


In the Django wiki, Simon Willison presents a trick to achieve the "self-extending template" effect. It isn't directly applicable if you're using the app_directories template loader, though.

Symlinking apps' templates directories inside a new directory might do a trick.