π 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
β Output Structure:
My Website
Home Page
This content is coming from child template.
Footer Section
π§ How It Works
- Browser calls
home() - Django loads
home.html home.htmlextendsbase.html- Content replaces
{% block content %} - Final HTML is rendered
π₯ Key Difference from Post 14
| Concept | Post 14 | Post 15 |
|---|---|---|
| Focus | Layout creation | Inheritance logic |
| Templates | Multiple pages | Single inheritance example |
| Concept depth | Basic | Concept 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
- Create another page
about.htmlusing same base - Add second block (title block)
- Add navigation menu in base
- 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
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
