liveBook · Manning

Template inheritance

As explained in Django in Action

Template inheritance is a templating technique in which a child template extends a parent template and overrides or fills its defined blocks.

Template inheritance keeps repeated page structure in one parent template. Child templates extend that parent and fill only the named blocks they need, so a site-wide change can reach many pages without copy-and-paste surgery. section 3.0 section 3.10

A Django inheritance hierarchy works like this:

  • Create a base template such as base.html containing the shared HTML structure and placeholder blocks. section 3.8 section 3.8.1
  • Mark replaceable regions with paired {% block name %} and {% endblock %} tags. section 3.8
  • Begin a child with {% extends "base.html" %}; matching child blocks replace the parent blocks while the rest of the structure remains inherited. section 3.8 section 3.8.1
  • Point the view’s render() call at the child template, then request the page to see the inherited result. section 3.8.1

A smaller News page

base.html defines title and content blocks. news2.html extends it, changes the title with {{ block.super }}: News, and supplies a News heading plus a loop over news items. The view renders news2.html, while styling added to base.html applies to every inheriting template. section 3.8.1

Figure 3.4 Page inheritance for component reuse
figure

Use the inheritance hierarchy to visualize how a base page can pass structure through intermediate layouts to a concrete page. section 3.8

How Django in Action covers it

How deep it goes
Explains named blocks, parent-child hierarchy, overrides, view selection, and shared styling without descending into template-engine internals.
Examples
Uses RiffMates base.html and news2.html to demonstrate title overrides, content blocks, news loops, and shared styling.
What you can do after
Apply it via Django templates to build reusable page layouts and override only page-specific regions.
How it connects
Connects template inheritance with template blocks, template composition, the render shortcut, and included templates.

Also explained in

Compare how 3 books explain template inheritance →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.