Overriding template blocks without copy/pasting entire template files

210 views
Skip to first unread message

Josh Smeaton

unread,
Jun 16, 2020, 1:03:53 AM6/16/20
to Django developers (Contributions to Django itself)
Something that has bugged me for awhile is the requirement to copy and paste an entire template when you would just like to override a single block. This arises mostly when overriding admin templates, like `admin/base.html`.

In my ideal world, I'd be able to do something like this:

    # myapp/templates/admin/base.html
   
{% override "admin/base.html" %}
   
{% block footer %}this site is restricted, blah blah legal text blah{% endblock %}


And then the template loading system would find the next `admin/base.html` in the chain and use my overrides.

There is prior art too. https://pypi.org/project/django-apptemplates/ allows you to override a template from a specific app using this syntax:

    # myapp/templates/admin/base.html
    
{% extends "admin:admin/base.html" %}
    
{% block footer %}this site is restricted, blah blah legal text blah{% endblock %}


I think this kind of functionality should be included within Django itself. If others agree, should there be a new name such as override, or would overloading extends be good enough?

Carlton Gibson

unread,
Jun 16, 2020, 1:41:29 AM6/16/20
to Django developers (Contributions to Django itself)
Hi Josh.


It leverages the extends tag, re-using the same template name:

    # myapp/templates/admin/base.html
   
{% extends "admin/base.html" %}

   
{% block footer %}this site is restricted, blah blah legal text blah{% endblock %}

So as long as myapp/templates are loaded before contrib.admin's then it you should see the result you're after.

#15053 references django-overextends as the influence.


Kind Regards,

Carlton

Josh Smeaton

unread,
Jun 16, 2020, 1:54:11 AM6/16/20
to Django developers (Contributions to Django itself)

Changed 5 years ago by Preston Timmons


🤦‍♀️

Thanks Carlton - I guess it has *only* been 5 years though :)

Josh Smeaton

unread,
Jun 16, 2020, 2:29:27 AM6/16/20
to Django developers (Contributions to Django itself)
Upon reflection, would you agree that this should be documented a little better? The release notes at the time had a vague deprecation note about `supports_recursion` being removed, and the main docs https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#std:templatetag-extends have no such explanation about recursive inheritance either.

I pointed out this thread to a few people in my team, and none of them were aware of this fix/feature, and they were all using 3rd party packages for support.

Funnily enough, https://docs.djangoproject.com/en/3.0/ref/contrib/admin/#overriding-vs-replacing-an-admin-template seems to suggest this was always possible, so I'm quite confused.

Carlton Gibson

unread,
Jun 16, 2020, 3:07:00 AM6/16/20
to Django developers (Contributions to Django itself)
Yes! Absolutely. There's even a ticket for that: https://code.djangoproject.com/ticket/29336

There was a PR, but we couldn't get it down to size... (Review)

Arvind Nedumaran

unread,
Jun 16, 2020, 8:16:31 AM6/16/20
to django-d...@googlegroups.com
Could you elaborate on how this is different from extending a base template and re-defining the necessary blocks?
#base.html
{%
block title %}
{{ section.title }}{% endblock %}
.....

{% extends "base.html" %}
{% block title %} My custom title that isn't section.title {% endblock %}
....
This much is already possible right?


--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/6cf43971-1ea9-4b93-9a35-aaab5908327co%40googlegroups.com.

Florian Apolloner

unread,
Jun 16, 2020, 9:16:45 AM6/16/20
to Django developers (Contributions to Django itself)
If you look at the mails beyond the first one, you will see that Josh & Carlton already came to that conlusion ;)


On Tuesday, June 16, 2020 at 2:16:31 PM UTC+2, Arvind Nedumaran wrote:
Could you elaborate on how this is different from extending a base template and re-defining the necessary blocks?
#base.html
{%
block title %}
{{ section.title }}{% endblock %}
.....

{% extends "base.html" %}
{% block title %} My custom title that isn't section.title {% endblock %}
....
This much is already possible right?


On Tue, Jun 16, 2020 at 10:34 AM Josh Smeaton <josh....@gmail.com> wrote:
Something that has bugged me for awhile is the requirement to copy and paste an entire template when you would just like to override a single block. This arises mostly when overriding admin templates, like `admin/base.html`.

In my ideal world, I'd be able to do something like this:

    # myapp/templates/admin/base.html
   
{% override "admin/base.html" %}
   
{% block footer %}this site is restricted, blah blah legal text blah{% endblock %}


And then the template loading system would find the next `admin/base.html` in the chain and use my overrides.

There is prior art too. https://pypi.org/project/django-apptemplates/ allows you to override a template from a specific app using this syntax:

    # myapp/templates/admin/base.html
    
{% extends "admin:admin/base.html" %}
    
{% block footer %}this site is restricted, blah blah legal text blah{% endblock %}


I think this kind of functionality should be included within Django itself. If others agree, should there be a new name such as override, or would overloading extends be good enough?

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.

Arvind Nedumaran

unread,
Jun 16, 2020, 9:20:25 AM6/16/20
to django-d...@googlegroups.com
Something must be faulty with my email delivery I suppose. I sent that email a good 8 hours ago. :)

Onward,
Arvind

Sent from Outlook Mobile


From: django-d...@googlegroups.com <django-d...@googlegroups.com> on behalf of Florian Apolloner <f.apo...@gmail.com>
Sent: Tuesday, June 16, 2020 6:46:45 PM
To: Django developers (Contributions to Django itself) <django-d...@googlegroups.com>
Subject: Re: Overriding template blocks without copy/pasting entire template files
 
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/89b8d89a-d7e4-44fa-a5b8-cb8e1c910785o%40googlegroups.com.

Florian Apolloner

unread,
Jun 16, 2020, 10:10:56 AM6/16/20
to Django developers (Contributions to Django itself)
Ah, might be that you got hold up in a moderation queue or similar. Google groups is weird sometimes -- sorry!

Arvind Nedumaran

unread,
Jun 16, 2020, 10:12:26 AM6/16/20
to django-d...@googlegroups.com
No worries 👍

Sent from Outlook Mobile


Sent: Tuesday, June 16, 2020 7:40:56 PM
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/a357fb57-55b3-4f65-bf59-cf64c3e412bbo%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages