Template Inheritance

Template inheritance in Django lets you define a single base template with the shared layout of your site — the header, footer, and navigation — and then have child templates extend it and fill in only the parts that change, so you never repeat your HTML skeleton.

Learn Template Inheritance in our free Django course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Django course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

In this lesson you'll build a base template with named blocks, write child pages that extend it, extend a parent block with block.super, and pull in reusable partials with the include tag.

A base template holds the structure every page on your site shares — the doctype, the head, the navigation bar, and the footer. Inside it you mark the regions that child pages are allowed to replace using {' '} tags. Every block has a name and ends with {' '} .

A typical base defines at least two blocks: a {' '} in the head for the page title, and a {' '} in the body where each page drops its main markup.

Anything outside a block — the header, nav, and footer here — is fixed and appears on every page. Anything inside a block is the default ; a child can override it or leave it as-is.

A child template starts with the {' '} tag — and it must be the very first line of the file. After that, the child only needs to redefine the blocks it wants to change. Any block it leaves out keeps the default from the base.

Sometimes you want to add to the parent block instead of replacing it. Calling {'{ }'} inside a child block outputs whatever the parent block contained, so you can keep it and extend it:

Here the title becomes About — My Site : the child's text plus the parent's default, pulled in by {'{ }'} .

Inheritance shares the outer skeleton; the {' '} tag shares inner snippets. A partial is a small standalone template — a card, an alert, a navbar — that you drop into a page wherever you need it. Unlike extends, you can include many partials, and anywhere in the file.

You include it and pass variables with the with keyword. Each value becomes a variable inside the partial:

The same card.html is rendered twice with different data — write the markup once, reuse it everywhere.

A child page should override the content region. Replace ___ with the block name so the renderer fills the content slot.

The {' '} tag has text, comments, or blank lines before it.

✅ Fix: make {' '} the very first line of the file, with nothing above it.

You opened a {' '} but never closed it, or the names don't match up.

✅ Fix: close every block with {' '} — optionally name it, e.g. {' '} .

In a child template, anything outside a block is silently ignored — only block content renders.

✅ Fix: put every child element inside a {' '} that the base actually defines.

Build a tiny renderer that takes a base skeleton plus a child's block overrides and produces the final page — exactly what Django's inheritance does for you.

Lesson 14 complete — your templates are DRY!

You can now build a base template with named blocks, extend it from child pages, override or augment blocks with block.super, and pull in reusable partials with the include tag.

🚀 Up next: URL Namespacing — organise your URLs so app names never collide.

Practice quiz

What does template inheritance let you do?

  • Run Python code directly inside templates
  • Define a base layout once and override only the parts that change
  • Automatically minify HTML output
  • Connect templates to the database

Answer: Define a base layout once and override only the parts that change. Inheritance lets a child template extend a base and override named blocks, keeping HTML DRY.

Which tag makes a template a child of base.html?

  • {% extends 'base.html' %}
  • {% include 'base.html' %}
  • {% block base.html %}
  • {% import 'base.html' %}

Answer: {% extends 'base.html' %}. {% extends 'base.html' %} declares the template inherits from base.html.

Where must the extends tag appear in a child template?

  • Anywhere inside the body block
  • Right before the first block tag
  • After the doctype declaration
  • As the very first line of the file

Answer: As the very first line of the file. The {% extends %} tag must be the first thing in the file or Django raises a TemplateSyntaxError.

What defines an overridable region in a base template?

  • {% region %} ... {% endregion %}
  • {% slot %} ... {% endslot %}
  • {% block name %} ... {% endblock %}
  • {{ block name }}

Answer: {% block name %} ... {% endblock %}. A {% block name %} ... {% endblock %} marks a region a child template can override.

What does {{ block.super }} output inside a child block?

  • The parent block's content, so the child can extend rather than replace it
  • The name of the parent template
  • Nothing — it is invalid syntax
  • The entire base template

Answer: The parent block's content, so the child can extend rather than replace it. {{ block.super }} emits the parent block's content, letting the child add to it.

How many parent templates can a single template extend?

  • Up to three
  • Exactly one
  • Any number
  • None — extends is optional decoration

Answer: Exactly one. A template can extend exactly one base; to share more, nest your base templates.

Which tag renders another template's contents inline at that spot?

  • {% extends %}
  • {% block %}
  • {% super %}
  • {% include %}

Answer: {% include %}. {% include 'partial.html' %} drops another template's content inline where it appears.

In a child template, what happens to content placed outside any block?

  • It replaces the base's footer
  • It renders at the top of the page
  • It is silently ignored and does not render
  • It causes a TemplateSyntaxError

Answer: It is silently ignored and does not render. Only content inside blocks the base defines renders; markup outside a block is ignored.

How do you pass variables into an included partial?

  • With the 'with' keyword, e.g. {% include 'card.html' with title='Sales' %}
  • By listing them after a colon
  • Partials cannot receive variables
  • By calling {{ block.super }}

Answer: With the 'with' keyword, e.g. {% include 'card.html' with title='Sales' %}. The 'with' keyword passes named values that become variables inside the included partial.

What is the key difference between extends and include?

  • extends works only in the body; include works only in the head
  • include can only be used once per file
  • They are interchangeable aliases
  • extends inherits a base's whole structure with overridable blocks; include drops a snippet inline

Answer: extends inherits a base's whole structure with overridable blocks; include drops a snippet inline. extends pulls in a base layout and overrides blocks; include renders a reusable snippet at its location.