Dec. 19, 2025

More adventures in building template components in Django

I discovered a new pattern in using one of the latest features from Django 6.0, template-partials. I probably need to give Claude some credit along with Tailwind Plus for the initial HTML. Earlier this week I was building out a modal pattern for the latest project I am building. You click a button, a modal pops up and loads a form via HTMX. Then submit the form and modal closes with the response triggering other HTMX updates on the page. I was building this out in tandem with Claude Code, with the starting point being a modal from Tailwind Plus which I thought only included the modal, but they also included the button to trigger the modal as well. Well the AI took this and ran with and essentially ended up with a Django template that looks like this:


{% partialdef trigger %}
<button hx-get...>...</button>
{% endpartialdef trigger %}


{% partialdef modal %}
<dialog>
    <form>
        ...
    </form>
</dialog>
{% endpartialdef modal %}

I don't think I would have ever thought to have included the button section as a partial in this context, but I like the results since both can be used with the include tag at different points and you have locality of behaviour if you want to update some aspect that affects both. I'm less likely to need to jump around to every button to add an attribute for example.

This got me thinking though, where else can partials fit in? Part of the magic with partials is the hash reference in the string reference to a template file. This means partials can be used in templatetags, earlier this year I wrote about using simple_block_tag in combination with get_template to produce a component like interface to build about a navigation. With partials I can merge the 3 small template files into a single template file with 3 partial templates. I think this is just the start for partials and template tags and I'm keen to explore their intersection even more in the coming weeks and months.

Finally the other area which Carlton recently hinted at on Mastodon is in the area of Form rendering. Django 4.X saw the refactoring of Form rendering into the FormRenderer class. This class allows the specification of snippet template files to customise how various form and form elements are rendered at a variety of levels from global down to individual forms. Again we can specify partials here instead of individual templates. I need to work on this a bit more, but the hope is a single file of partials that is something of a spritesheet of form components instead of jumping around multiple files. We can then use partials to deduplicate these templates.

Here is a quick example of my PartialsFormRenderer:

from django.forms.renderers import TemplatesSetting

class PartialsFormRenderer(TemplatesSetting):
    form_template_name = "forms/form_snippet.html#form"
    field_template_name = "forms/form_snippet.html#field"

I am excited for what comes next and how much we could achieve with Django features alone!