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. πŸ‘¨β€πŸ‘©β€πŸ‘§


πŸ—οΈ Folder Setup

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

✨ Step 1: Create the Base Template

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>

🧠 What's Happening Here?

  • {% block title %} and {% endblock %} β€” You can override this title in child templates.
  • {% block content %} β€” This is where each page will put its own content.

✨ Step 2: Create a Child Template

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 %}

πŸ” Reuse, Reuse, Reuse

  • 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.

🧩 Bonus: You Can Add More Blocks!

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 %}

πŸ‘€ Final Look When Rendered

When the user visits the / URL, Django will:

  1. Use home.html.
  2. See {% extends 'my_app/base.html' %}.
  3. Fill in the title and content blocks.
  4. Combine everything to show a beautiful, complete webpage!

🏁 Summary – Why Use Template Inheritance?

βœ… Keeps your code clean
βœ… Makes updates faster
βœ… Avoids repetition
βœ… Follows DRY principle
βœ… Easy to maintain large websites


🧠 Memory Trick:

πŸ” "If you repeat it, template inheritance can beat it!"


⚠️ **GitHub.com Fallback** ⚠️