Django

Use {% include %} to Reuse Header and Footer Templates

๐Ÿ“Œ 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

TagPurpose
{% 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

๐Ÿ‘‰ http://127.0.0.1:8000/


โœ… Output Structure:

My Django Website
Home | About | Contact

Home Page
Welcome to the homepage.

ยฉ 2026 My Website

๐Ÿง  How It Works

  1. Template loads home.html
  2. {% include 'header.html' %} inserts header
  3. Main content is displayed
  4. {% 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

  1. Create sidebar.html and include it
  2. Include header in multiple pages
  3. Combine include + extends
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

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