Django

Implement Template Inheritance using {% extends %} and {% block %}

πŸ“Œ Introduction

Template inheritance allows us to:

  • Reuse layout
  • Avoid repetition
  • Maintain clean code

πŸ‘‰ Two main tags:

  • {% extends %} β†’ inherit base template
  • {% block %} β†’ define replaceable sections

🎯 Program Statement

πŸ‘‰ Implement template inheritance using {% extends %} and {% block %}.


βš™οΈ Step 1: Create Base Template


πŸ“ File: base.html

πŸ”Ή Path:

myproject/templates/base.html

πŸ”Ή Code:

<!DOCTYPE html>
<html>
<head>
<title>Django Inheritance</title>
</head>
<body>

<h1>My Website</h1>
<hr>

{% block content %}
<!-- Child content will come here -->
{% endblock %}

<hr>
<footer>Footer Section</footer>

</body>
</html>

🧠 Key Concept

πŸ‘‰ This line defines a placeholder:

{% block content %}

βš™οΈ Step 2: Create Child Template


πŸ“ File: home.html

πŸ”Ή Path:

myproject/templates/home.html

πŸ”Ή Code:

{% extends 'base.html' %}

{% block content %}
<h2>Home Page</h2>
<p>This content is coming from child template.</p>
{% endblock %}

🧠 Key Concept

πŸ‘‰ This line connects child to base:

{% extends 'base.html' %}

βš™οΈ Step 3: 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 4: 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 5: Run Server

python manage.py runserver

🌐 Step 6: Output

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


βœ… Output Structure:

My Website

Home Page
This content is coming from child template.

Footer Section

🧠 How It Works

  1. Browser calls home()
  2. Django loads home.html
  3. home.html extends base.html
  4. Content replaces {% block content %}
  5. Final HTML is rendered

πŸ”₯ Key Difference from Post 14

ConceptPost 14Post 15
FocusLayout creationInheritance logic
TemplatesMultiple pagesSingle inheritance example
Concept depthBasicConcept clarity

⚠️ Common Errors


❌ {% extends %} not at top

πŸ‘‰ Must be first line:

{% extends 'base.html' %}

❌ Block name mismatch

πŸ‘‰ Must match:

{% block content %}

❌ Template not found

πŸ‘‰ Check correct folder:

templates/base.html

πŸ§ͺ Practice Questions

  1. Create another page about.html using same base
  2. Add second block (title block)
  3. Add navigation menu in base
  4. Change footer dynamically

🎀 Viva Questions & Answers


1. What is template inheritance?

Template inheritance allows one template to reuse another template’s layout. It helps in reducing code duplication.


2. What is {% extends %}?

It is used to inherit a parent template. It must be written at the top of the file.


3. What is {% block %}?

It defines a replaceable section in a template. Child templates override this block.


4. Can we have multiple blocks?

Yes, multiple blocks can be defined like header, footer, sidebar. Each block can be overridden.


5. What happens if block is not overridden?

The default content in base template will be displayed.


6. Why is inheritance important?

It keeps code DRY (Don’t Repeat Yourself) and improves maintainability.


7. Can templates extend multiple templates?

No, a template can extend only one parent template.


8. What is base template?

A base template contains common structure shared across pages.


9. What is reusable UI?

A UI that can be used across multiple pages without rewriting code.


10. What happens if {% extends %} is missing?

Template will not inherit layout and will render independently.


πŸ”— Navigation

πŸ‘‰ Next Post: Pass Dictionary and List to Template (Context Data)
πŸ‘‰ 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 *