TExcelTAD
Home › Django › Django Template Inheritance

Django Template Inheritance

Every page of a site usually shares the same skeleton: <head>, navigation bar, footer. Copying that HTML into every template would be a maintenance nightmare. Django solves it with two complementary tools:

  • Inheritance — {% extends %} and {% block %}: a base template defines the layout with named holes, and child templates fill in the holes.
  • Inclusion — {% include %}: insert a small reusable fragment (a card, a menu) into any template.

A base template with blocks

A typical project has a templates/base.html like this:

html
<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head>
  <title>{% block title %}Bookshelf{% endblock %}</title>
</head>
<body>
  <nav>Home | Books | About</nav>
  <main>{% block content %}{% endblock %}</main>
  <footer>{% block footer %}© Bookshelf{% endblock %}</footer>
</body>
</html>

Each {% block name %} is a placeholder with optional default content. A child template starts with {% extends "base.html" %} and overrides only the blocks it cares about — everything outside blocks in the child is ignored.

Extending a template

The playground keeps template "files" in a dictionary through the locmem loader, so we can build a small family of templates:

python · try it yourself
import django
from django.conf import settings
TEMPLATE_FILES = {
    "base.html": (
        "<title>{% block title %}Bookshelf{% endblock %}</title>\n"
        "<nav>Home | Books</nav>\n"
        "<main>{% block content %}{% endblock %}</main>\n"
        "<footer>{% block footer %}(c) Bookshelf{% endblock %}</footer>"
    ),
    "book_list.html": (
        '{% extends "base.html" %}'
        "{% block title %}All books - Bookshelf{% endblock %}"
        "{% block content %}{% for b in books %}[{{ b }}]{% endfor %}{% endblock %}"
    ),
}
settings.configure(TEMPLATES=[{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "OPTIONS": {"loaders": [("django.template.loaders.locmem.Loader", TEMPLATE_FILES)]},
}])
django.setup()
from django.template.loader import render_to_string

print(render_to_string("book_list.html", {"books": ["Dune", "Emma"]}))
<title>All books - Bookshelf</title>
<nav>Home | Books</nav>
<main>[Dune][Emma]</main>
<footer>(c) Bookshelf</footer>

The child replaced title and content; footer kept the base template's default.

Reusing the parent's content: block.super

Inside an overriding block, {{ block.super }} inserts whatever the parent put there. It's ideal for adding to a block instead of replacing it — extra CSS files, breadcrumbs or a longer title:

python · try it yourself
import django
from django.conf import settings
TEMPLATE_FILES = {
    "base.html": "<head>{% block styles %}<link href='site.css'>{% endblock %}</head>",
    "shop.html": "{% extends 'base.html' %}{% block styles %}{{ block.super }}<link href='shop.css'>{% endblock %}",
}
settings.configure(TEMPLATES=[{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "OPTIONS": {"loaders": [("django.template.loaders.locmem.Loader", TEMPLATE_FILES)]},
}])
django.setup()
from django.template.loader import render_to_string

print(render_to_string("base.html"))
print(render_to_string("shop.html"))
<head><link href='site.css'></head>
<head><link href='site.css'><link href='shop.css'></head>

Multi-level inheritance

Inheritance chains can be as deep as you like. A common three-level setup is: base.html (whole site) → section_base.html (e.g. the blog's sidebar) → individual pages.

python · try it yourself
import django
from django.conf import settings
TEMPLATE_FILES = {
    "base.html": "[site]{% block body %}{% endblock %}[/site]",
    "blog/base.html": "{% extends 'base.html' %}{% block body %}[blog]{% block post %}{% endblock %}[sidebar]{% endblock %}",
    "blog/post.html": "{% extends 'blog/base.html' %}{% block post %}<h1>{{ title }}</h1>{% endblock %}",
}
settings.configure(TEMPLATES=[{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "OPTIONS": {"loaders": [("django.template.loaders.locmem.Loader", TEMPLATE_FILES)]},
}])
django.setup()
from django.template.loader import render_to_string

print(render_to_string("blog/post.html", {"title": "Hello Django"}))
[site][blog]<h1>Hello Django</h1>[sidebar][/site]

include: reusable fragments

{% include "name.html" %} renders another template with the current context at that spot. Pass extra variables with with, and add only to give the fragment nothing but those variables:

python · try it yourself
import django
from django.conf import settings
TEMPLATE_FILES = {
    "partials/book_card.html": "<div class='card'>{{ book.title }} by {{ book.author|default:'?' }}{% if shop %} @ {{ shop }}{% endif %}</div>",
    "list.html": (
        "{% for b in books %}{% include 'partials/book_card.html' with book=b %}\n{% endfor %}"
        "{% include 'partials/book_card.html' with book=featured only %}"
    ),
}
settings.configure(TEMPLATES=[{
    "BACKEND": "django.template.backends.django.DjangoTemplates",
    "OPTIONS": {"loaders": [("django.template.loaders.locmem.Loader", TEMPLATE_FILES)]},
}])
django.setup()
from django.template.loader import render_to_string

books = [{"title": "Kafka on the Shore", "author": "Murakami"}, {"title": "Beowulf"}]
featured = {"title": "Norwegian Wood", "author": "Murakami"}
print(render_to_string("list.html", {"books": books, "featured": featured, "shop": "Main St"}))
<div class='card'>Kafka on the Shore by Murakami @ Main St</div>
<div class='card'>Beowulf by ? @ Main St</div>
<div class='card'>Norwegian Wood by Murakami</div>

The last card used only, so it didn't see shop.

Rules worth remembering

Rule Why
{% extends %} must be the first tag in the template Django needs to know the parent before anything else
Content outside {% block %} in a child is ignored only blocks are merged into the parent
Block names must be unique within a template each hole is identified by name
You can repeat the name in the end tag: {% endblock content %} improves readability in long templates
include is rendered at run time with the current context fragments see the same variables (unless only)

Tip: Use inheritance for the page skeleton and include for components repeated inside pages. In projects, keep base.html in a project-wide templates/ folder listed in TEMPLATES["DIRS"].

Test yourself

Where must {% extends "base.html" %} appear in a child template?

  1. Anywhere
  2. As the first template tag
  3. At the very end
  4. Inside a block

What does {{ block.super }} do?

  1. Renders the child block twice
  2. Inserts the parent template's content for that block
  3. Deletes the block
  4. Includes base.html

What does only do in {% include 'card.html' with book=b only %}?

  1. Renders the include once
  2. Passes only the listed variables to the fragment
  3. Prevents escaping
  4. Caches the template