Wednesday, 7 January 2026

Django Module #3 – URLs & Views

Django Module 3 – URLs & Views

๐Ÿ“˜ Django Tutorial – Module 3

Topic: URL Routing & Views


๐ŸŽฏ Learning Objectives

  • Understand Django URL routing
  • Create function-based views
  • Map URLs to views
  • Test URLs in browser

๐Ÿ”น What is a View?

A view is a Python function that receives a request and returns a response (HTML, text, JSON, etc.).

๐Ÿ”น Step 1: Create a View


# blog/views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello Django!")
  

๐Ÿ”น Step 2: Create App-Level URLs


# blog/urls.py
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
]
  

๐Ÿ”น Step 3: Include App URLs in Project


# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', include('blog.urls')),
]
  

๐Ÿ”น Step 4: Run Server


python manage.py runserver
  

๐ŸŒ Output

Open browser and visit:


http://127.0.0.1:8000/
  

๐Ÿงช Student Practice

  • Create another view about()
  • Map it to /about
  • Display your name and department

❓ Viva / Exam Questions

  1. What is URL routing in Django?
  2. What is the role of urls.py?
  3. Difference between view and template?
๐ŸŽ“ Teaching Tips:Ask students to trace URL → View → Response
Modify response text live
Show 404 error when URL is missing

No comments:

Post a Comment

Django #7a Project Sample

Employee Management System – Django (HRMS) Employee Management System (Django + MySQL) Roles: Admin, HR, Employee 1....