Open In App

Views In Django

Last Updated : 25 Nov, 2025
Comments
Improve
Suggest changes
33 Likes
Like
Report

In Django’s MVT architecture, views handle user requests and return responses. They act as a bridge between models (data) and templates (UI), deciding what the user sees in the browser.

A view is a Python function or class that receives an HTTP request, processes data, and returns a response. This response can be:

  • An HTML page (rendered from a template)
  • A redirect (to another URL)
  • An error (like 404 Not Found)
  • JSON, XML, images, or any browser-compatible content
url
Django Views

Illustration of How to create and use a Django view using an Example. Consider a project named geeksforgeeks having an app named geeks. 

After you have a project ready, we can create a view in geeks/views.py,

Python
# import HttpResponse from django
from django.http import HttpResponse
# import datetime
import datetime

# create a function-based view
def geeks_view(request):
    # fetch current date and time
    now = datetime.datetime.now()
    # convert to string
    html = "Time is {}".format(now)
    # return HttpResponse
    return HttpResponse(html)

Let's step through this code one line at a time: 

  • First, we import the class HttpResponse from the django.http module, along with Python’s datetime library.
  • Next, we define a function called geeks_view. This is the view function. Each view function takes an HttpRequest object as its first parameter, which is typically named request.
  • The view returns an HttpResponse object that contains the generated response. Each view function is responsible for returning an HttpResponse object.

Let's get this view to working, in geeks/urls.py,

Python
from django.urls import path

# importing views from views.py
from .views import geeks_view

urlpatterns = [
    path('', geeks_view),
]

Now, visit http://127.0.0.1:8000/.

django-view-example
Snapshot of Output

Django View Types

Django provides two main types of views to handle web requests and return responses:

  1. Function-Based Views (FBVs): Written as Python functions. They are simple and ideal for small features or straightforward logic, such as displaying a list of items, handling form submissions, or showing a single detail page.
  2. Class-Based Views (CBVs): Written as Python classes. They offer better organization, reusability, and support for object-oriented features. They are suited for complex applications, reusable components, or features that require handling multiple HTTP methods cleanly.
2
Types of views

Both FBVs and CBVs support the common CRUD operations: Create, Retrieve/Detail, Update, Delete.

Applications and Features:

Function-Based Views are typically used for simple features, such as:

  • Displaying a list of items
  • Showing details of a single object
  • Handling basic form submissions

Class-Based Views are best for complex or reusable features, such as:

  • Generic CRUD operations
  • Multi-step forms
  • Features requiring multiple HTTP methods in a clean, organized structure
Suggested Quiz
5 Questions

What is a view in Django?

  • A

    A database table definition

  • B

    A static HTML file

  • C

    A function or class that receives an HTTP request and returns an HTTP response

  • D

    A URL configuration file

Explanation:

Django views handle incoming requests, run logic, and return responses (for example HTML pages or redirects).

Which of these is a type of view supported by Django?

  • A

    Script-Based View (SBV)

  • B

    Static View (SV)

  • C

    Function-Based View (FBV)

  • D

    Template-Only View (TOV)

Explanation:

Django supports views written as simple Python functions to handle requests.

Which view type offers more structure, reusability, and built-in support for common tasks like CRUD in Django?

  • A

    Class-Based Views

  • B

    Function-Based Views

  • C

    Template-Based Views

  • D

    Static Views

Explanation:

Class-Based Views (CBVs) organise logic in classes, support object-oriented features, and simplify common patterns like list/detail/create/update/delete.

What kinds of tasks are Function-Based Views typically best suited for?

  • A

    Complex multi-step forms and heavy logic

  • B

    Template rendering only

  • C

    Database migrations

  • D

    Simple and straightforward requests like displaying a page or handling a form submission

Explanation:

FBVs are simple and direct, making them ideal for small features such as showing a list or handling a basic form.

Which of these is a built-in generic view class in Django designed to display a list of objects from the database?

  • A

    DetailView

  • B

    DeleteView

  • C

    ListView

  • D

    FormView

Explanation:

ListView (a class-based view) automatically fetches records and renders them in a list template, reducing boilerplate.

Quiz Completed Successfully
Your Score :   2/5
Accuracy :  0%
Login to View Explanation
1/5 1/5 < Previous Next >

Explore