MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Template Inheritance0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
4.1Django Forms & Validation4.2Template Engine Basics4.3Template Inheritance
HomeTutorialsDjango for Backend DevelopmentTemplate Inheritance
Function-Based Views (FBV)

Template Inheritance

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

Writing redundant HTML boilerplate across dozens of views—such as repetitive <head> tags, navigation bars, CSS stylesheets, and footers—violates the Don't Repeat Yourself (DRY) principle. Django's Template Inheritance solves this using a skeleton layout architecture with overridable {% block %} sections.


1. The Master Base Layout (base.html)

Create a master layout defining the site-wide HTML skeleton and content slots:

1{# templates/base.html #}
2{% load static %}
3<!DOCTYPE html>
4<html lang="en">
5<head>
6 <meta charset="UTF-8">
7 <meta name="viewport" content="width=device-width, initial-scale=1.0">
8
9 {# Overridable Title Block with default fallback #}
10 <title>{% block title %}Enterprise Django Portal{% endblock %}</title>
11
12 {# Tailwind CSS CDN / Static Assets #}
13 <script src="https://cdn.tailwindcss.com"></script>
14 <link rel="stylesheet" href="{% static 'css/styles.css' %}">
15
16 {# Extra Head Block for per-page CSS #}
17 {% block extra_head %}{% endblock %}
18</head>
19<body class="bg-slate-950 text-slate-100 min-h-screen flex flex-col font-sans">
20 
21 {# Global Header #}
22 <header class="border-b border-slate-800 bg-slate-900 px-6 py-4 flex justify-between items-center">
23 <a href="{% url 'articles:home' %}" class="font-bold text-cyan-400 text-lg">CloudNexus</a>
24 <nav class="flex gap-4 text-sm">
25 <a href="{% url 'articles:home' %}" class="hover:text-cyan-300">Articles</a>
26 {% if user.is_authenticated %}
27 <span>{{ user.email }}</span>
28 <a href="{% url 'logout' %}" class="text-rose-400">Logout</a>
29 {% else %}
30 <a href="{% url 'login' %}" class="text-cyan-400">Sign In</a>
31 {% endif %}
32 </nav>
33 </header>
34 
35 {# Global Messages / Alerts (Flash Notifications) #}
36 {% if messages %}
37 <div class="container mx-auto p-4 space-y-2">
38 {% for message in messages %}
39 <div class="p-3 rounded text-xs font-semibold {% if message.tags == 'error' %}bg-rose-950 border border-rose-800 text-rose-300{% else %}bg-emerald-950 border border-emerald-800 text-emerald-300{% endif %}">
40 {{ message }}
41 </div>
42 {% endfor %}
43 </div>
44 {% endif %}
45 
46 {# Primary Content Block #}
47 <main class="flex-1 container mx-auto p-6">
48 {% block content %}
49 {# Child templates inject custom page content here #}
50 {% endblock %}
51 </main>
52 
53 {# Global Footer #}
54 <footer class="border-t border-slate-800 p-6 text-center text-xs text-slate-500">
55 &copy; 2026 Enterprise Django Systems. All rights reserved.
56 </footer>
57 
58 {# Extra Scripts Block for per-page JS #}
59 {% block extra_scripts %}{% endblock %}
60</body>
61</html>

2. Extending the Layout in Child Templates

Child templates declare which base template they inherit from using {% extends %} as the very first line:

1{# templates/articles/article_detail.html #}
2{% extends "base.html" %}
3 
4{# Override Page Title #}
5{% block title %}{{ article.title }} | CloudNexus{% endblock %}
6 
7{# Inject Page Content #}
8{% block content %}
9 <article class="max-w-3xl mx-auto bg-slate-900 border border-slate-800 p-8 rounded-2xl">
10 <header class="border-b border-slate-800 pb-4 mb-6">
11 <h1 class="text-3xl font-extrabold text-white mb-2">{{ article.title }}</h1>
12 <div class="text-xs text-slate-400 flex gap-4">
13 <span>By {{ article.author.username }}</span>
14 <span>Published: {{ article.created_at|date:"M d, Y" }}</span>
15 <span>Views: {{ article.view_count }}</span>
16 </div>
17 </header>
18 
19 <div class="prose prose-invert max-w-none text-slate-300 leading-relaxed">
20 {{ article.content|linebreaks }}
21 </div>
22 </article>
23{% endblock %}

3. Preserving Parent Content with {{ block.super }}

If you want to append content to a block rather than completely overwriting it, use {{ block.super }}:

1{% block extra_head %}
2 {{ block.super }} {# Retains any CSS loaded in parent base.html #}
3 <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/prismjs/themes/prism-tomorrow.min.css">
4{% endblock %}

4. Reusable Partials with {% include %}

For repeated UI fragments (like an article card or pagination bar), use {% include %}:

1{# Includes reusable component template #}
2{% include "articles/components/article_card.html" with item=article %}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What tag must be the very first tag in a child template that inherits from a master layout?
Question 2
What is the purpose of the {{ block.super }} variable in a template block?
Question 3
What is the architectural difference between {% extends %} and {% include %}?
Question 4
How do you configure Django to find templates stored in a global root /templates/ directory?
Question 5
What does the |linebreaks filter do in Django templates?
Next Lesson

Function-Based Views (FBV)

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Template Engine BasicsFunction-Based Views (FBV)

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext