Session Overview

  • URL Routing
  • Views
  • HttpResponse
  • render() Function
  • Multiple Pages Navigation
Important: Django does not create the file blog/urls.py automatically. We must create this file manually inside the blog application.

URL Routing

URL Routing maps a URL to a specific View function.

When a user visits a URL, Django checks the URL configuration and executes the corresponding View.

File Location
blog/urls.py

Create a new file named urls.py inside the blog application.

Code: blog/urls.py
from django.urls import path
from . import views

urlpatterns = [

]
Add URL Route
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
]
Register Application URLs

Open the project's main URL file and include the blog application's URLs.

BlogProject/urls.py
Code: BlogProject/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]
Note: BlogProject/urls.py forwards requests to blog/urls.py.
Important: Previously, Django was showing its default welcome page. After adding the following route:
path('', include('blog.urls'))
the root URL (/) is now handled by our application. Therefore, Django's default welcome page is replaced by our own Home Page.

Views

A View is a Python function that receives a request and returns a response.

File Location
blog/views.py
Code: blog/views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django")
Create Another View
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django")

def about(request):
    return HttpResponse("About Page")
Update URLs
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
    path('about/', views.about),
]
Test URLs
http://127.0.0.1:8000/

http://127.0.0.1:8000/about/
Result: Home URL displays "Welcome to Django" About URL displays "About Page"
Note: Currently, our Views are returning plain text using HttpResponse(). In the next section, we will learn how to display HTML pages using the render() function.

HttpResponse

HttpResponse is used to send a response from a View to the browser.

It is useful when we want to display simple text content.

File Location
blog/views.py
Example
from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to Django")
HTML Content Using HttpResponse
from django.http import HttpResponse

def about(request):
    return HttpResponse("<h1>About Us</h1>")
Browser Output
About Us
Limitation: Writing large HTML code inside HttpResponse() is difficult and not recommended.

render() Function

The render() function is used to display HTML pages from a View.

This is the most commonly used approach in Django applications.

Step 1 - Create Templates Folder
blog
│
├── templates
│
└── views.py
Step 2 - Create HTML File
blog/templates/home.html
Code: home.html
<h1>Welcome To Django</h1>
<p>This page is rendered using render().</p>
Step 3 - Update View
from django.shortcuts import render

def home(request):
    return render(request,'home.html')
Step 4 - Open Browser
http://127.0.0.1:8000/
Result: Django loads the home.html file and displays it in the browser.
What is request? The request parameter contains information sent by the browser to the server. Every Django View receives a request object automatically. Examples of information available in request:
  • URL requested by the user
  • Form data submitted by the user
  • Query string values
  • User information
Note: We have placed home.html inside the application's templates folder. Django automatically searches the templates folder for HTML files, therefore we only write:
return render(request,'home.html')
We do not need to write:
return render(request,'templates/home.html')

Multiple Pages Navigation

A real website contains multiple pages such as Home, About and Contact.

Step 1 - Create About Page
demo/templates/about.html
<h1>About Us</h1>
<p>Welcome to our About Page.</p>
Step 2 - Create Contact Page
demo/templates/contact.html
<h1>Contact Us</h1>
<p>Welcome to our Contact Page.</p>
Step 3 - Update Views
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 4 - Update URLs
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home),
    path('about/', views.about),
    path('contact/', views.contact),
]
Test URLs
http://127.0.0.1:8000/

http://127.0.0.1:8000/about/

http://127.0.0.1:8000/contact/

Quick Revision

URL Routing
  • path()
  • Connect URL to View
  • urls.py
Views
  • Python Function
  • Receives Request
  • Returns Response
HttpResponse
  • Simple Text Output
  • Quick Testing
  • Returns Response
render()
  • Display HTML Pages
  • Uses Templates
  • Most Common Method
Templates
  • Create templates Folder
  • Store HTML Files
  • home.html, about.html
Project Flow
  • URL → View
  • View → Template
  • Template → Browser
Common Mistakes
  • Create urls.py manually inside the application.
  • Register application URLs in the project's main urls.py.
  • Add the application name to INSTALLED_APPS.
  • Create a folder named templates (not template).
  • Select the correct Virtual Environment Interpreter in VS Code.
  • Restart the server after making major configuration changes.

Assignment

Complete the following tasks:
  1. Create an application named student.
  2. Register the application in INSTALLED_APPS.
  3. Create a student/urls.py file and configure URL routing.
  4. Create three pages: Home, Courses and Contact.
  5. Use render() to display all three HTML pages and verify navigation using URLs.