liveBook · Manning

Template block

As explained in Django in Action

A template block is a named section of a template delimited by paired template tags that can be replaced by a child template.

A template block names a replaceable section of a Django template. Paired {% block %} and {% endblock %} tags mark the section, allowing child templates to reuse a parent page while changing selected content. section 3.8

A common pattern uses base.html for shared HTML such as the page title and body structure. A child template extends it and overrides only the blocks it needs, keeping repeated markup in one place. section 3.8 section 3.8.1

RiffMates news page

base.html defines title and content blocks. news2.html uses {% extends "base.html" %}, replaces the title with {{ block.super }}: News, and supplies an <h1> plus a loop over news items. Updating the view to render news2.html makes the inherited page available at /news/. section 3.8.1

Naming the closing tag, as in {% endblock content %}, improves readability, especially when blocks are nested. {{ block.super }} preserves the parent block’s content while adding to it. section 3.8.1

Figure 3.4 Page inheritance for component reuse
figure

Page inheritance can be arranged in layers: a base page, optional variations such as navigation, and concrete site pages. section 3.8

How Django in Action covers it

How deep it goes
Defines block syntax, inheritance, named closing tags, block.super, and a working view-template update.
Examples
Uses RiffMates base.html and news2.html to demonstrate title and content overrides, loops, and inherited content.
What you can do after
Apply Django template inheritance to build shorter pages that share structure and site-wide styling.
How it connects
Links template blocks with template inheritance, base templates, child templates, extends tags, and context variables.

Also explained in

Compare how 2 books explain template block →

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.