Template Inheritance with extends
In this page:
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.
<!-- 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.
<!-- 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.
{% 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. #}
- Forgetting that {% extends %} must be the very first line in a child template -- anything before it causes an error.
- Naming a {% block %} in the child template that doesn't match any block name defined in the parent, so the content is silently dropped.
- Not calling {{ block.super }} when they actually wanted to keep the parent block's content and add to it, instead of replacing it entirely.
- {% 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: