π 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
- Add a new page βServicesβ
- Create view and template for it
- Add link in all pages
- 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
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
