Django

Create Multiple URL Routes using Templates

πŸ“Œ Introduction

In real web applications, we need multiple pages like Home, About, Contact.
In this program, we will:

  • Create multiple views
  • Map multiple URLs
  • Use templates (HTML files)

🎯 Program Statement

πŸ‘‰ Create multiple URL routes such as Home, About, and Contact pages.


βš™οΈ Step 1: Project & App Setup

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

βš™οΈ Step 2: Register App


πŸ“ File: settings.py

πŸ”Ή Path:

myproject/myproject/settings.py

πŸ”Ή Code:

INSTALLED_APPS = [
...
'myapp',
]

βš™οΈ Step 3: Configure Templates Folder


πŸ“ File: settings.py

πŸ”Ή Path:

myproject/myproject/settings.py

πŸ”Ή Update TEMPLATES:

import os

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
...
},
]

βš™οΈ Step 4: Create Templates Folder

Create folder manually:

myproject/templates/

βš™οΈ Step 5: Create HTML Files


πŸ“ File: home.html

πŸ”Ή Path:

myproject/templates/home.html

πŸ”Ή Code:

<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to Home Page</h1>

<a href="/about/">About</a> |
<a href="/contact/">Contact</a>
</body>
</html>

πŸ“ File: about.html

πŸ”Ή Path:

myproject/templates/about.html

πŸ”Ή Code:

<!DOCTYPE html>
<html>
<head>
<title>About</title>
</head>
<body>
<h1>About Page</h1>
<p>This is About Page of Django Project.</p>

<a href="/">Home</a> |
<a href="/contact/">Contact</a>
</body>
</html>

πŸ“ File: contact.html

πŸ”Ή Path:

myproject/templates/contact.html

πŸ”Ή Code:

<!DOCTYPE html>
<html>
<head>
<title>Contact</title>
</head>
<body>
<h1>Contact Page</h1>
<p>Email: example@gmail.com</p>

<a href="/">Home</a> |
<a href="/about/">About</a>
</body>
</html>

βš™οΈ Step 6: Create Views


πŸ“ File: views.py

πŸ”Ή Path:

myproject/myapp/views.py

πŸ”Ή Code:

from django.shortcuts import render

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

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

def contact(request):
return render(request, 'contact.html')

βš™οΈ Step 7: 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),
path('about/', views.about),
path('contact/', views.contact),
]

βš™οΈ Step 8: Run Server

python manage.py runserver

🌐 Step 9: Output

Open:

πŸ‘‰ http://127.0.0.1:8000/ β†’ Home
πŸ‘‰ http://127.0.0.1:8000/about/ β†’ About
πŸ‘‰ http://127.0.0.1:8000/contact/ β†’ Contact


🧠 Explanation

  • render() β†’ loads HTML template
  • Each page β†’ separate view
  • Each URL β†’ mapped to view
  • Templates β†’ stored in templates/ folder

⚠️ Common Errors


❌ TemplateDoesNotExist

πŸ‘‰ Ensure:

  • Folder name = templates
  • Path added in settings.py

❌ Page not found

πŸ‘‰ Check:

path('about/', views.about)

❌ Navigation links not working

πŸ‘‰ Always use /about/ not about


πŸ§ͺ Practice Questions

  1. Add a new page β€œServices”
  2. Create view and template for it
  3. Add link in all pages
  4. Make navigation using buttons instead of links

🎀 Viva Questions & Answers


1. What is a template in Django?

A template is an HTML file used to display data to the user. It separates presentation from logic.


2. What is the use of render()?

The render() function loads a template and returns it as an HTTP response. It combines data with HTML.


3. Why do we use templates folder?

Templates folder stores HTML files used by Django. It helps organize UI separately from backend logic.


4. What is multiple URL routing?

It means mapping different URLs to different views. It allows multiple pages in a web application.


5. What is the role of views in multi-page apps?

Views define what each page will display. Each page usually has a separate view function.


6. Can multiple URLs use same template?

Yes, different URLs can render the same template with different data. This improves reusability.


7. What is DIRS in TEMPLATES?

DIRS tells Django where to look for template files. It usually includes the templates folder path.


8. What happens if template path is wrong?

Django throws TemplateDoesNotExist error. It means it cannot find the HTML file.


9. What is BASE_DIR?

BASE_DIR is the root directory of the project. It helps in defining paths dynamically.


10. Why do we separate HTML and Python?

To maintain clean architecture. HTML handles UI while Python handles logic.


πŸ‘‰ Next Post: Display Variable in Template (Dynamic 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 *