Django Views

Every time a user visits a page on your website, something has to decide what to show them. In Django, that "something" is called a View. A view is simply a Python function that receives a request from the browser and sends back a response.

What is a View?

A View is a Python function written inside views.py of your app. It takes a request as input (what the user asked for) and returns a response (what the user sees on screen).

Example: Think of a waiter in a restaurant. A customer (browser) places an order (request). The waiter (view) goes to the kitchen, gets the food, and brings it back to the customer (response). The waiter does not cook the food — he just handles the communication between customer and kitchen.

Types of Views in Django

Django has two main types of views:

  • Function-Based Views (FBV): Simple Python functions. Best for beginners and straightforward pages.
  • Class-Based Views (CBV): Python classes with built-in reusable logic. Better for complex pages with repeated patterns.

In this lesson we will focus on Function-Based Views as they are easier to understand first.

Writing Your First View

Open school/views.py and write your first view function:


from django.http import HttpResponse

def home(request):
    return HttpResponse("Welcome to My School Website!")

Let us understand each line:

  • from django.http import HttpResponse — We import the tool that lets us send a plain text or HTML response back to the browser.
  • def home(request): — We define a function called home. It receives request which contains all the information about what the user sent (browser type, URL, form data, etc.).
  • return HttpResponse(...) — We send back a simple text response to the browser.
Example: The request object is like an envelope a customer sends to your shop. Inside the envelope, they tell you what they want. The HttpResponse is your reply envelope that you send back with what they asked for.

Connecting a View to a URL

A view function is useless unless you connect it to a URL. This is done in urls.py. Open myschool/urls.py and update it like this:


from django.contrib import admin
from django.urls import path
from school import views

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),   # <-- Home page at /
]

Now when someone visits http://127.0.0.1:8000/, Django runs the home view and shows "Welcome to My School Website!"

Sending an HTML Response

Instead of plain text, you can return proper HTML from a view:


from django.http import HttpResponse

def home(request):
    return HttpResponse("<h1>Welcome to My School</h1><p>Best school in town!</p>")
Example: Sending plain text is like handing someone a plain notepad. Sending HTML is like handing them a nicely designed printed brochure. Both carry information, but one looks much better.

Creating Multiple Views

You can write as many views as you need — one for each page of your website:


from django.http import HttpResponse

def home(request):
    return HttpResponse("<h1>Home Page</h1>")

def about(request):
    return HttpResponse("<h1>About Us</h1>")

def contact(request):
    return HttpResponse("<h1>Contact Us</h1>")

Then connect each one to a URL in urls.py:


urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
    path('contact/', views.contact, name='contact'),
]
Example: Each view is like a different room in your house. The front door (URL) leads to the living room (home view), another door leads to the bedroom (about view), and another to the kitchen (contact view).

The request Object — What's Inside?

Every view function receives a request object automatically. This object carries useful information about the user's request:

  • request.method — Was it a GET (page visit) or POST (form submission)?
  • request.GET — Data sent through the URL (like search terms).
  • request.POST — Data submitted through a form.
  • request.user — The logged-in user (if any).

def home(request):
    print(request.method)   # Prints: GET
    return HttpResponse("Home Page")

Returning a 404 Error from a View

Sometimes a page does not exist. Django provides a built-in way to return a proper "Page Not Found" error:


from django.http import Http404

def student_detail(request, student_id):
    if student_id > 100:
        raise Http404("Student does not exist")
    return HttpResponse(f"Showing details for student {student_id}")
Example: It is like going to a hotel reception and asking for Room 999, but the hotel only has 100 rooms. The receptionist politely says "That room does not exist" — that is exactly what a 404 response does.

Quick Recap


# A basic view in school/views.py
from django.http import HttpResponse

def home(request):
    return HttpResponse("Hello from the Home page!")

# Connect it in urls.py
from school import views
path('', views.home, name='home'),

Leave a Comment

Your email address will not be published. Required fields are marked *