How to make a reusable template in Django?

If you need to use {% block %} you can only do that via the {% extend %} approach. Otherwise, you can use {% include 'some.html' %} to include a bit of HTML in multiple places.


What you're looking for, is {% include "template.html"%} from Django docs.


The unofficial Django Reusable App Conventions recommends using these block names:

  • {% block title %}
  • {% block extra_head %}
  • {% block body %}
  • {% block menu %}
  • {% block content %}
  • {% block content_title %}
  • {% block header %} {% block footer %}
  • {% block body_id %} {% block body_class %}
  • {% block [section]_menu %} {% block page_menu %}

If everyone stuck to these conventions, it should make this problem easier. Follow the link to see the description of each block.


The most flexible way to reuse template fragments is to define an inclusion_tag. You can pass arguments to your custom tag, process them a bit in Python, then bounce back to a template. Direct inclusion only works for fragments that don't depend on the surrounding context.

Quick example from the docs:

In app/templatetags/poll_extras.py register the tag with a decoration:

from django import template
register = template.Library()

@register.inclusion_tag('results.html')
def show_results(poll):
    choices = poll.choice_set.all()
    return {'choices': choices}

In app/templates/results.html:

<ul>
{% for choice in choices %}
    <li> {{ choice }} </li>
{% endfor %}
</ul>

Calling the tag:

{% load poll_extras %}
{% show_results poll %}