๐ Introduction
In Django, instead of writing header/footer repeatedly, we use:
๐ {% include %}
This helps:
- Reuse code
- Maintain consistency
- Reduce duplication
๐ฏ Program Statement
๐ Use {% include %} to reuse header and footer templates.
๐ง Concept Overview
| Tag | Purpose |
|---|---|
{% include %} | Insert another template |
{% extends %} | Inherit full layout |
๐ Difference:
- include โ partial reuse
- extends โ full layout reuse
โ๏ธ Step 1: Create Header Template
๐ File: header.html
๐น Path:
myproject/templates/header.html
๐น Code:
<header>
<h1>My Django Website</h1>
<nav>
<a href="/">Home</a> |
<a href="/about/">About</a> |
<a href="/contact/">Contact</a>
</nav>
<hr>
</header>
โ๏ธ Step 2: Create Footer Template
๐ File: footer.html
๐น Path:
myproject/templates/footer.html
๐น Code:
<hr>
<footer>
<p>ยฉ 2026 My Website | All Rights Reserved</p>
</footer>
โ๏ธ Step 3: Create Main Template
๐ File: home.html
๐น Path:
myproject/templates/home.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<!-- Include Header -->
{% include 'header.html' %}
<h2>Home Page</h2>
<p>Welcome to the homepage.</p>
<!-- Include Footer -->
{% include 'footer.html' %}
</body>
</html>
โ๏ธ Step 4: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
โ๏ธ Step 5: 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),
]
โ๏ธ Step 6: Run Server
python manage.py runserver
๐ Step 7: Output
โ Output Structure:
My Django Website
Home | About | Contact
Home Page
Welcome to the homepage.
ยฉ 2026 My Website
๐ง How It Works
- Template loads
home.html {% include 'header.html' %}inserts header- Main content is displayed
{% include 'footer.html' %}inserts footer
๐ฅ Key Concept
{% include 'file.html' %}
๐ Inserts another template at that position
โ ๏ธ Common Errors
โ Template not found
๐ Check:
templates/header.html
templates/footer.html
โ Wrong include syntax
โ Wrong:
{% include header.html %}
โ Correct:
{% include 'header.html' %}
โ Path issue
๐ Use correct relative path
๐งช Practice Questions
- Create
sidebar.htmland include it - Include header in multiple pages
- Combine include + extends
- Pass data to included template
๐ค Viva Questions & Answers
1. What is {% include %} in Django?
It is used to insert one template inside another template. It helps in reusing common components.
2. What is difference between include and extends?
include inserts partial templates, while extends creates a full parent-child relationship.
3. Why use include?
To avoid repetition of common sections like header and footer. It improves maintainability.
4. Can we include multiple templates?
Yes, we can include multiple templates in a single file.
5. Can include be used inside extends?
Yes, it is commonly used with extends to create modular layouts.
6. What happens if included file is missing?
Django raises a template error.
7. Can we pass data to included templates?
Yes, using with keyword (advanced concept).
8. Is include dynamic or static?
It is dynamic because it renders template at runtime.
9. Where is include mostly used?
Header, footer, sidebar, navigation menus.
10. Is include better than copy-paste?
Yes, it ensures consistency and reduces redundancy.
๐ Navigation
๐ Next Post: Use {% if %} Condition for Eligibility
๐ 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
