Template Partials in Django 6.0
Django 6.0 introduces template partials, named and reusable template fragments. You can define a partial block once and include it in multiple places within the same template or from other templates, without needing a separate file.
Syntax
Define a partial with {% partial name %} and include it with {% partialblock name %}. Partials accept parameters so they can be reused in different contexts.
htmldjango
{# templates/base.html #}
{# Defining a reusable partial #}
{% partial card %}
<div class="card">
<h3>{{ card_title }}</h3>
<p>{{ card_body }}</p>
{% if card_link %}
<a href="{{ card_link }}">Learn more</a>
{% endif %}
</div>
{% endpartial %}
{# Usage in the same template #}
<div class="grid">
{% partialblock card with card_title="Product A" card_body="Description..." card_link="/a" %}
{% partialblock card with card_title="Product B" card_body="Another product" %}
</div>
Practical Example: List Component
Partials are especially useful for creating recurring UI components like badges, alerts, or list items. They are a better alternative to {% include %} which required separate files.
htmldjango
{# templates/dashboard.html #}
{% extends "base.html" %}
{% partial stat_badge %}
<span class="badge badge-{{ badge_color }}">
{{ badge_label }}: {{ badge_value }}
</span>
{% endpartial %}
{% block content %}
<h1>Dashboard</h1>
<div class="stats">
{% partialblock stat_badge with badge_color="green" badge_label="Active" badge_value=active_count %}
{% partialblock stat_badge with badge_color="red" badge_label="Errors" badge_value=error_count %}
{% partialblock stat_badge with badge_color="blue" badge_label="Total" badge_value=total_count %}
</div>
{% endblock %}
