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:
<!-- 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:
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:
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.
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:
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
includefor components repeated inside pages. In projects, keepbase.htmlin a project-widetemplates/folder listed inTEMPLATES["DIRS"].
Test yourself
Where must {% extends "base.html" %} appear in a child template?
- Anywhere
- As the first template tag
- At the very end
- Inside a block
What does {{ block.super }} do?
- Renders the child block twice
- Inserts the parent template's content for that block
- Deletes the block
- Includes base.html
What does only do in {% include 'card.html' with book=b only %}?
- Renders the include once
- Passes only the listed variables to the fragment
- Prevents escaping
- Caches the template