How to Use Multiple Templates in Django
We have now added only the template we need for the home page. Now let's create the necessary templates for the about page and contact page. After that you can link between these pages
Step 1
Your home page template will look like this :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page</title>
</head>
<body>
<h1>Home</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati error
iusto quos veritatis tenetur eveniet totam cum neque quas voluptatibus quod dicta,
pariatur exercitationem sit perferendis reprehenderit ea fugit voluptatum.</p>
</body>
</html>
Step 2
Your about page template will look like this :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>About page</title>
</head>
<body>
<h1>About Us</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati error
iusto quos veritatis tenetur eveniet totam cum neque quas voluptatibus quod dicta,
pariatur exercitationem sit perferendis reprehenderit ea fugit voluptatum.</p>
</body>
</html>
Step 3
Your contact page template will look like this :-
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact Page</title>
</head>
<body>
<h1>Contact Us</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati error
iusto quos veritatis tenetur eveniet totam cum neque quas voluptatibus quod dicta,
pariatur exercitationem sit perferendis reprehenderit ea fugit voluptatum.</p>
</body>
</html>
Step 4
Along with the home page, we can create views for the about page and contact page
from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
def index(request):
template = loader.get_template("index.html")
return HttpResponse(template.render({},request))
def about(request):
template = loader.get_template("about.html")
return HttpResponse(template.render({},request))
def contact(request):
template = loader.get_template("contact.html")
return HttpResponse(template.render({},request))
Step 5
Along with home page we can add urls for about page and contact page in urls.py file
from django. urls import path
from . import views
urlpatterns=[
path('',views.index,name='index'),
path('about',views.about,name='about'),
path('contact',views.contact,name='contact'),
]
Step 6
Go to the url 'http://127.0.0.1:8000/about' to see the About page

Step 7
Go to the url 'http://127.0.0.1:8000/contact' to see the Contact page

Step 8
Links can be given from the home page to other pages
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Home Page</title>
</head>
<body>
<nav>
<a href="/">Home</a>
<a href="/about">About</a>
<a href="/contact">Contact</a>
</nav>
<h1>Home</h1>
<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Obcaecati error
iusto quos veritatis tenetur eveniet totam cum neque quas voluptatibus quod dicta,
pariatur exercitationem sit perferendis reprehenderit ea fugit voluptatum.
</p>
</body>
</html>
Step 9
After giving the links the home page will look like this:-
