New in Django 6.0
The partialdef tag was added in Django
6.0 and is not available in earlier versions.
Defines a reusable template fragment that can be rendered multiple times within the same template using the partial tag. This helps avoid repetition and maintain consistent output.
Basic Usage
Define a partial with a name, then render it later with the partial tag:
Template
{% partialdef user-info %}
<div id="user-info-{{ user.username }}">
<h3>{{ user.name }}</h3>
<p>{{ user.bio }}</p>
</div>
{% endpartialdef %}
{# Render the partial elsewhere in the template #}
{% partial user-info %}
For clarity, you can include the partial name in the closing tag:
{% partialdef user-info %}
...
{% endpartialdef user-info %}
Arguments
partial_name(required) – A valid template identifier for the fragment.inline(optional) – When included, the partial is both defined and immediately rendered in place.
Inline Partials
Use the inline argument to define and immediately render a partial:
{% partialdef card inline %}
<div class="card">
<h3>{{ title }}</h3>
<p>{{ content }}</p>
</div>
{% endpartialdef %}
{# The card is already rendered above, but can be rendered again: #}
{% partial card %}
Direct Access via Template Loading
Partials can be accessed directly when loading or including templates using hash syntax:
{% include "mytemplate.html#user-info" %}
Context
Partials have access to the current template context. They work within loops and with tags, allowing context to be adjusted for each rendering.
Template
{% partialdef button inline %}
<button class="btn btn-{{ style }}">{{ label }}</button>
{% endpartialdef %}
{% with style="primary" label="Save" %}
{% partial button %}
{% endwith %}
{% with style="danger" label="Delete" %}
{% partial button %}
{% endwith %}
Result
<button class="btn btn-primary">Save</button> <button class="btn btn-danger">Delete</button>

Commentary
Template partials are a welcome addition to Django's template system. They fill a gap between simple variable reuse (with the
withtag) and full template includes (with theincludetag).Use partials when you need to repeat a small fragment multiple times within the same template. For fragments that need to be shared across multiple templates, continue using
include.