๐ Introduction
In Django, instead of repeating HTML on every page, we use a base template.
๐ Benefits:
- Reusable layout
- Cleaner code
- Easy maintenance
We will create:
- Header
- Navigation menu
- Footer
- Extend it in other pages
๐ฏ Program Statement
๐ Create a base template layout containing header, footer, and navigation.
โ๏ธ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
โ๏ธ Step 2: Register App
๐ File: settings.py
Path:
myproject/myproject/settings.py
Code:
INSTALLED_APPS = [
...
'myapp',
]
โ๏ธ Step 3: Configure Templates
๐ File: settings.py
Path:
myproject/myproject/settings.py
Code:
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
โ๏ธ Step 4: Create Base Template
๐ File: base.html
๐น Path:
myproject/templates/base.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Django Website</title>
</head>
<body>
<!-- Header -->
<header>
<h1>My Django Website</h1>
</header>
<!-- Navigation -->
<nav>
<a href="/">Home</a> |
<a href="/about/">About</a> |
<a href="/contact/">Contact</a>
</nav>
<hr>
<!-- Dynamic Content Block -->
{% block content %}
{% endblock %}
<hr>
<!-- Footer -->
<footer>
<p>ยฉ 2026 My Website | All Rights Reserved</p>
</footer>
</body>
</html>
๐ง Explanation
{% block content %}โ placeholder- Child templates will replace this part
- Header + Footer remain same
โ๏ธ Step 5: Create Views
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
def about(request):
return render(request, 'about.html')
def contact(request):
return render(request, 'contact.html')
โ๏ธ Step 6: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home),
path('about/', views.about),
path('contact/', views.contact),
]
โ๏ธ Step 7: Create Child Templates
๐ File: home.html
๐น Path:
myproject/templates/home.html
๐น Code:
{% extends 'base.html' %}
{% block content %}
<h2>Home Page</h2>
<p>Welcome to the homepage.</p>
{% endblock %}
๐ File: about.html
๐น Path:
myproject/templates/about.html
๐น Code:
{% extends 'base.html' %}
{% block content %}
<h2>About Page</h2>
<p>This is the about page.</p>
{% endblock %}
๐ File: contact.html
๐น Path:
myproject/templates/contact.html
๐น Code:
{% extends 'base.html' %}
{% block content %}
<h2>Contact Page</h2>
<p>Email: example@gmail.com</p>
{% endblock %}
โ๏ธ Step 8: Run Server
python manage.py runserver
๐ Step 9: Output
๐ Home:
http://127.0.0.1:8000/
๐ About:
http://127.0.0.1:8000/about/
๐ Contact:
http://127.0.0.1:8000/contact/
โ Output Structure:
- Header
- Navigation
- Page Content (changes)
- Footer
๐ง How It Works
- Base template defines layout
- Child templates extend it
- Content inserted using block
- Layout reused across pages
โ ๏ธ Common Errors
โ Missing {% extends %}
๐ Always write at top:
{% extends 'base.html' %}
โ Block not defined
๐ Must match:
{% block content %}
โ Wrong template name
๐ Check:
'base.html'
๐งช Practice Questions
- Add sidebar in base template
- Add CSS styling
- Highlight active menu
- Add login/logout links
๐ค Viva Questions & Answers
1. What is template inheritance in Django?
Template inheritance allows reuse of common layout using a base template. Child templates extend it and override specific parts.
2. What is {% block %}?
It defines a section that can be replaced by child templates. It acts as a placeholder.
3. What is {% extends %}?
It tells Django that a template inherits from another template. It must be written at the top.
4. Why use base template?
It avoids code repetition and ensures consistency across pages. It simplifies maintenance.
5. Can we have multiple blocks?
Yes, multiple blocks like header, footer, sidebar can be defined. Each can be overridden separately.
6. What happens if block is not defined?
Content will not be inserted and may not display. Blocks must match exactly.
7. Can we nest templates?
Yes, templates can extend other templates in multiple levels.
8. What is reusable layout?
A layout used across multiple pages without rewriting code. Base template provides this.
9. Where is base template stored?
It is stored in the templates folder, usually at project level.
10. Why is template inheritance important?
It is essential for building scalable and maintainable web applications.
๐ Navigation
๐ Next Post: Template Inheritance with Multiple Blocks
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
