---
title: '{% partialdef %}'
description: defines a reusable template fragment.
date: '2026-08-02'
categories:
  - Template Tag
  - Utility
canonical: https://www.djangotemplatetagsandfilters.com/tags/partialdef/
doc_link: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/#partialdef
---

# {% partialdef %}

defines a reusable template fragment.

## Documentation

Defines a reusable template fragment that can be rendered multiple times within the same template using the [`partial`](/tags/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

```django
{% 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:

```django
{% 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:

```django
{% 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:

```django
{% 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

```django
{% 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

```django
<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 [`with`](/tags/with/) tag) and full template includes (with the [`include`](/tags/include/) tag).

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`.
