Skip to content

Template Inheritance (Extends)

Template inheritance is how you avoid repeating HTML like headers/navbars/footers.

diagram what a child template may and may not contain mermaid
A child does not add to the base, it fills holes in it. Anything the child writes outside a block is discarded without a warning -- the base decides the layout, and only the named blocks are yours. Calling super() inside a block is how you keep the base content and add to it rather than replacing it.

templates/base.html:

html
<!doctype html>
<html>
  <head>
    <title>{% block title %}My Site{% endblock %}</title>
  </head>
  <body>
    <nav>
      <a href="/">Home</a>
      <a href="/about">About</a>
    </nav>
 
    <main>
      {% block content %}{% endblock %}
    </main>
  </body>
</html>

templates/home.html:

html
{% extends "base.html" %}
 
{% block title %}Home{% endblock %}
 
{% block content %}
  <h1>Welcome!</h1>
{% endblock %}
  • One place to change your global layout
  • Consistent design
  • Easier to maintain as pages grow

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading