Template Inheritance - potatoscript/django GitHub Wiki
When you build websites, a lot of pages share the same structure:
β
Same header
β
Same footer
β
Same sidebar
But the content in the middle changes depending on the page. Instead of copying and pasting the same HTML over and over again... π©
β‘οΈ Template Inheritance lets you create a base template (the common structure) and other templates that extend it (like a child).
Just like how a child can inherit from a parent in a family, a template can inherit from another template in Django. π¨βπ©βπ§
Hereβs how your Django app folder should be organized:
my_project/
βββ my_app/
β βββ templates/
β β βββ my_app/
β β βββ base.html β π The parent template
β β βββ home.html β π§ Inherits from base
β β βββ about.html β π§ Inherits from base
This is the foundation of your site, like the layout blueprint.
π my_app/templates/my_app/base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
<header>
<h1>π PotatoScript Blog</h1>
<nav>
<a href="/">π Home</a> |
<a href="/about/">π©βπ» About</a>
</nav>
</header>
<main>
{% block content %}
<!-- Default content goes here -->
{% endblock %}
</main>
<footer>
<p>π° Made with Django β’ Β© 2025</p>
</footer>
</body>
</html>
-
{% block title %}
and{% endblock %}
β You can override this title in child templates. -
{% block content %}
β This is where each page will put its own content.
Now, letβs make a page that uses the base template and fills in its own stuff.
π my_app/templates/my_app/home.html
{% extends 'my_app/base.html' %}
{% block title %}Home - PotatoScript{% endblock %}
{% block content %}
<h2>π Welcome to PotatoScript!</h2>
<p>This is your homepage powered by Django Templates.</p>
<ul>
<li>π± Learn Python</li>
<li>π Master Django</li>
<li>π Share your knowledge</li>
</ul>
{% endblock %}
π my_app/templates/my_app/about.html
{% extends 'my_app/base.html' %}
{% block title %}About Us{% endblock %}
{% block content %}
<h2>π©βπ» About PotatoScript</h2>
<p>We are passionate about teaching Django in a fun and simple way!</p>
<p>Contact us if you love coffee and code βπ».</p>
{% endblock %}
- If you want to change the menu, do it once in
base.html
and it's updated everywhere! - All child templates only fill in the blanks, like content or page title.
Want to have a block for a sidebar, scripts, or styles?
Add this to base.html
:
<aside>
{% block sidebar %}
<p>Default sidebar content</p>
{% endblock %}
</aside>
Then in any child template:
{% block sidebar %}
<ul>
<li>π§ Python</li>
<li>π Web Dev</li>
</ul>
{% endblock %}
When the user visits the /
URL, Django will:
- Use
home.html
. - See
{% extends 'my_app/base.html' %}
. - Fill in the
title
andcontent
blocks. - Combine everything to show a beautiful, complete webpage!
β
Keeps your code clean
β
Makes updates faster
β
Avoids repetition
β
Follows DRY principle
β
Easy to maintain large websites
π "If you repeat it, template inheritance can beat it!"