← Back to Django Course | Chapter 3: Templates & Template Language | Lesson 6 of 10

Template Inheritance with extends

Template inheritance is like a photo frame: one base template holds the shared layout, and each page just swaps in its own picture.

Defining a Base Template

A base template defines the overall page structure (like <html>, navigation, footer) and marks replaceable regions with {% block %} tags.

Example: Defining a Base Template

This base.html defines two blocks -- title and content -- that child templates can fill in or override.

markup
<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head><title>{% block title %}My Site{% endblock %}</title></head>
<body>
  <nav>Home | About</nav>
  {% block content %}{% endblock %}
</body>
</html>
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Extending the Base Template

A child template starts with {% extends %} and only needs to redefine the blocks it wants to customize.

Note: {% extends %} must be the first tag in the file, before any other content.

Example: Extending the Base Template

home.html reuses base.html's full layout, replacing only the title and content blocks.

markup
<!-- templates/home.html -->
{% extends "base.html" %}

{% block title %}Home{% endblock %}

{% block content %}
  <h1>Welcome to the homepage!</h1>
{% endblock %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Keeping Parent Content with block.super

Calling {{ block.super }} inside a child block inserts the parent block's original content instead of fully replacing it.

Example: Keeping Parent Content with block.super

This keeps whatever the parent's content block defined and appends the extra paragraph after it.

markup
{% block content %}
  {{ block.super }}
  <p>Extra info shown only on this page.</p>
{% endblock %}
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting that {% extends %} must be the very first line in a child template -- anything before it causes an error.
  2. Naming a {% block %} in the child template that doesn't match any block name defined in the parent, so the content is silently dropped.
  3. Not calling {{ block.super }} when they actually wanted to keep the parent block's content and add to it, instead of replacing it entirely.
Chapter Summary
  • {% extends 'base.html' %} lets a child template inherit a parent template's layout.
  • {% block name %}...{% endblock %} defines a section in the parent that children can override.
  • A child template only needs to define the blocks it wants to change -- everything else falls back to the parent.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.