[go: up one dir, main page]

0% found this document useful (0 votes)
22 views3 pages

Django Template Inheritance Lab Manual

This lab manual demonstrates how to implement Django template inheritance by creating a base template and extending it with child templates. It outlines the steps for setting up a Django project, creating necessary templates, configuring URLs, and running the server. The objective is to showcase the benefits of reusing common layouts for improved code maintainability.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Django Template Inheritance Lab Manual

This lab manual demonstrates how to implement Django template inheritance by creating a base template and extending it with child templates. It outlines the steps for setting up a Django project, creating necessary templates, configuring URLs, and running the server. The objective is to showcase the benefits of reusing common layouts for improved code maintainability.

Uploaded by

ekta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Django Template Inheritance - Lab Manual

Objective:

To demonstrate how a base template can be extended by other templates using Django's template
inheritance mechanism.

Step 1: Django Project Setup

Run the following commands:


django-admin startproject mysite
cd mysite
python manage.py startapp myapp

Step 2: Register the App

Add 'myapp' to INSTALLED_APPS in mysite/settings.py.

Step 3: Create the Templates Folder Structure

Inside the myapp directory, create the following:


myapp/templates/myapp/base.html
myapp/templates/myapp/home.html
myapp/templates/myapp/about.html

Step 4: Create Base Template (base.html)

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
<header>
<h1>Welcome to My Website</h1>
<nav>
<a href="{% url 'home' %}">Home</a> |
<a href="{% url 'about' %}">About</a>
Django Template Inheritance - Lab Manual

</nav>
</header>
<hr>
{% block content %}{% endblock %}
<hr>
<footer>
<p>© 2025 My Website</p>
</footer>
</body>
</html>

Step 5: Create Child Templates (home.html and about.html)

Example: home.html
{% extends 'myapp/base.html' %}

{% block title %}Home - My Website{% endblock %}

{% block content %}
<h2>This is the Home Page</h2>
<p>Welcome to the homepage of our Django application.</p>
{% endblock %}

Example: about.html
{% extends 'myapp/base.html' %}

{% block title %}About - My Website{% endblock %}

{% block content %}
<h2>This is the About Page</h2>
<p>This page provides information about our website.</p>
{% endblock %}

Step 6: Configure URLs

In myapp/views.py:
Django Template Inheritance - Lab Manual

from django.shortcuts import render

def home(request):
return render(request, 'myapp/home.html')

def about(request):
return render(request, 'myapp/about.html')

In mysite/urls.py:
from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
path('about/', views.about, name='about'),
]

Step 7: Run the Server

Run the server using:


python manage.py runserver

Visit:
http://127.0.0.1:8000/ for Home Page
http://127.0.0.1:8000/about/ for About Page

Summary

Django template inheritance helps in reusing common layouts and improves code maintainability by using
base and child templates efficiently.

You might also like