Table of contents
1.
Introduction
2.
What is a Django DetailView?
3.
What is function-based Detail View?
4.
Function-Based Vs. Class-Based Detail View
5.
When to use function-based Detail View?
6.
Sample Django App
7.
Frequently Asked Questions
8.
 
9.
 
10.
 
11.
Key Takeaways
Last Updated: Jun 25, 2025

Function-Based Detail View in Django

Author Sneha Mallik
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Views in Django are Python functions or classes that receive a web request and return a web response in the Django framework. A basic HTTP response, an HTML template response, or an HTTP redirect response that transfers a user to another page are all possible responses. Django views provide the logic required to return information to the user in any manner. The logic that deals with views should be kept in the ‘views.py’ file as a best practice in a Django app. 

The detail view method shows the data for a particular database record. The Django DetailView is a view that displays a single instance from the model table. It is used to display information of a single item from the database and perform various tasks on that instance.

What is a Django DetailView?

The Django DetailView is a view that displays a single instance from the Model Table. It is used to display information from a single database entry and perform various tasks in that instance.

What is function-based Detail View?

A detail view in Django is a view (logic) that displays a particular instance of a database table with all the required details. It is used to view or display multiple data types on a single page, such as a user's profile. Django provides support to Detail View functionality. 

The function-based detail view has many lines of code and can be converted to a class-based detail view.

In multiple forms, we can additionally display selected fields based on the type of usage required. The slug is frequently used to define the detail view rather than the id.

This blog focuses on the function-based detail view in Django, which includes Django Models. We will work on a project with models and multiple instances to display the detail view.

Function-Based Vs. Class-Based Detail View

Let us see the fundamental differences between Function-Based and Class-Based Detail View:

Function-Based Detail View Class-Based Detail View
Simple to read, comprehend, and apply.It's difficult to put into practice and even more challenging to read.
It has an explicit code flowIt has an implicit code flow.
Redundancy in the code and difficulty in extending itMixins can be used to extend class-based views and add more functionality.
Decorators are used in a straightforward manner.In view decorators, an extra import or method override is required.

When to use function-based Detail View?

Django includes several generic views based on functions to help with common tasks. Detail View is one of them.

If we want to view the details of a single model instance, we should use the Detail View. We should not use DetailView if the page contains a form and the object has been created or updated. Form View, Create View, and Update View are suitable for working with forms and creating or updating objects.

Sample Django App

Consider that we are working on a library project. 

django-admin startproject func_detail 
cd func_detail 
python3 manage.py startapp func_detail_app

 

func_detail_app/settings.py:

INSTALLED_APPS = [
  'django.contrib.admin',
  'django.contrib.auth',
  'django.contrib.contenttypes',
  'django.contrib.sessions',
  'django.contrib.messages',
  'django.contrib.staticfiles',
  'func_detail_app',
]
You can also try this code with Online Python Compiler
Run Code

 

We will have a Book model in the ‘func_detail_app’ application.

func_detail_app/models.py:

# To import the Django model
from django.db import models

 
# To declare the new model having name 'Book'
class Book(models.Model):
  
  # The fields of the 'Book' model
  name = models.CharField(max_length=255)
  isbn_number = models.CharField(max_length=13)

  # To rename the instances of the 'Book' model with the title name
  def __str__(self):
      return self.name
You can also try this code with Online Python Compiler
Run Code

 

After creating the Book model, we'll need to run two commands to establish a database.

python3 manage.py makemigrations func_detail_app
python manage.py migrate

 

Now, using shell, let's make some instances of this model, run from bash.

python manage.py shell

 

Now, we'll make a list of all of the Book objects to be stored in the database. We will create a detail view using a function-based view called ‘index’. We will add the following code to the views.

func_detail_app/views.py:

from django.shortcuts import render

# Create your views here.
from .models import Book


def index(request,id):
  context = {}
  context["book"]=Book.objects.get(id=id)
  return render(request, 'func_detail_app/index.html', context)
You can also try this code with Online Python Compiler
Run Code

 

We'll add the following URL for func_detail_app to include the detailed view above.

func_detail_app/urls.py:

from .views import index
from django.urls import path

urlpatterns = [
  path('<id>', index),
]
You can also try this code with Online Python Compiler
Run Code

 

We need to ensure that the project URLs include the func_detail_app URLs. Otherwise, we will include it.

func_detail/urls.py:

from django.contrib import admin
from django.urls import include, path

urlpatterns = [
  path('', include('func_detail_app.urls')),
  path('admin/', admin.site.urls),
]
You can also try this code with Online Python Compiler
Run Code

 

Then we will develop an HTML template to view the data.

func_detail_app/templates/func_detail_app/index.html:

<!DOCTYPE html>

<head>
  <title>Book Information</title>
</head>

<body>
  <h2>Book Information</h2>
  
   <hr>
  
  <div class="main">
   
   <h4>Book Name</h4>  {{ book.name }}
  <h4>ISBN Number</h4>  {{ book.isbn_number }}<br/>
   
  </div>
</body>

</html>

 

We have two more books added to the Book list by the Admin that we learned in the class-based List View method.

Therefore, we can now get the details of book 1 by giving the path ‘http://127.0.0.1:8000/1’.

Output

We can now get the details of book 2 by giving the path ‘http://127.0.0.1:8000/2’ and so on.

Output

Frequently Asked Questions

  1. How many forms of detail views does Django have?
    Function-based detail views and class-based detail views are the two forms of detail views in Django.
     
  2. List down some pros and cons of the function-based detail view.

Pros

Cons

  • Implementation is simple.
  • Easy to read the code.
  • The code flow is explicit.
  • The usage of decorators is straightforward.
  • The code cannot be reused.
  • It is hard to extend the code.
  • The handling of HTTP methods is done via conditional branching.

 

 

 

Key Takeaways

In this blog, we have learned the concepts of function-based detail views in Python, which displays a particular instance of a database table with all the required details. Views are crucial for web development, especially when utilizing MVT (Model View Template) frameworks like Django. This essential ability is beneficial for Python developers who work as Django developers, backend engineers, or full-stack engineers. Detail View also helps avoid multiple lines of code and provides a better separation of concerns.

Go through The Basics of Django Framework to deeply understand the concept of Django to get a hold of the function-based detail view in Django

Credits: GIPHY

Happy Developing!

Live masterclass