2️⃣ 🌐 Core Django Concepts
Estimated reading: 3 minutes 35 views

🧱 Django Add Master Template – Build Reusable Layouts with Template Inheritance (2025)

🧲 Introduction – What Is a Master Template?

In Django, a master template (also called a base template) provides a common layout structure—like a header, footer, and navigation bar—that all other pages can inherit. This promotes DRY (Don’t Repeat Yourself) coding, faster updates, and consistent design across your site.

🎯 In this guide, you’ll learn:

  • How to create a base (master) template
  • How to extend it in child templates
  • How to use {% block %} and {% extends %}
  • Best practices for maintainable template architecture

📁 Step 1: Create the Master Template

Inside your app or a global templates/ directory:

templates/base.html

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
    <link rel="stylesheet" href="{% static 'css/styles.css' %}">
</head>
<body>

<header>
    <h1>My Django Site</h1>
    <nav>
        <a href="/">Home</a> |
        <a href="/blog/">Blog</a>
    </nav>
</header>

<hr>

<main>
    {% block content %}{% endblock %}
</main>

<hr>

<footer>
    <p>&copy; 2025 My Django App</p>
</footer>

</body>
</html>

✅ This becomes your layout foundation with replaceable content blocks.


🧱 Step 2: Use extends in a Child Template

Example for a blog post page:

templates/blog/post_detail.html

{% extends 'base.html' %}

{% block title %}{{ post.title }}{% endblock %}

{% block content %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
<small>Published on {{ post.published|date:"F j, Y" }}</small>
{% endblock %}

✔️ Only the content that changes needs to be written.


🌐 Step 3: Set the Template Directory in settings.py

Make sure Django can find the base.html:

# settings.py
TEMPLATES = [
    {
        ...
        'DIRS': [BASE_DIR / "templates"],
        ...
    }
]

✅ If using app-level templates, structure should be:
yourapp/templates/yourapp/base.html


📋 Common Template Blocks

Block NamePurpose
titlePage title (inside <title>)
contentMain body content
sidebarOptional side menu or widget
scriptsJavaScript at the bottom

✅ Best Practices

  • Name blocks clearly: content, title, footer, etc.
  • Don’t repeat layout HTML in every file—use {% extends %}
  • Centralize common scripts and CSS in base.html
  • Add {% block scripts %} at the end of <body> for optional JS

📌 Summary – Recap & Next Steps

🔍 Key Takeaways:

  • A master template ensures layout consistency and reusability
  • Use {% extends 'base.html' %} to inherit from it
  • Define {% block %} sections that child templates can override

⚙️ Real-World Relevance:
Master templates power scalable websites, blogs, admin panels, and e-commerce platforms—saving hours in design and code duplication.


❓ Frequently Asked Questions (FAQ)

❓ What is the difference between include and extends?

extends is for inheritance.
include is for inserting reusable chunks (e.g., header, footer).


❓ Can I have multiple master templates?

✅ Yes. You can have different base templates for admin, public pages, dashboards, etc.


❓ Where should I place the base.html?

✅ Either in a global templates/ folder or inside app/templates/app/.


❓ How do I load static files like CSS in base templates?

✅ Use {% load static %} and:

<link href="{% static 'css/style.css' %}" rel="stylesheet">

❓ Can I nest blocks inside other blocks?

✅ Yes, but be cautious. Nesting works, but overdoing it may reduce readability.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Django Add Master Template

Or Copy Link

CONTENTS
Scroll to Top