Detail View - Function based Views Django
A Detail View is used to show information about one specific record, such as a single post, product, or user profile.
- Retrieves one record from the database using its ID or another unique value.
- Sends that record to a template for rendering.
- Displays the full details of the selected item.
- Provides a clear view of one specific database entry.
Example: Consider a project named 'geeksforgeeks' having an app named 'geeks'. After you have a project and an app, let's create a model of which we will be creating instances through our view.
In geeks/models.py
from django.db import models
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):
# fields of the model
title = models.CharField(max_length = 200)
description = models.TextField()
# renames the instances of the model
def __str__(self):
return self.title
After creating this model, we need to run two commands in order to create Database for the same.
Python manage.py makemigrations
Python manage.py migrate
Create instances of this model using the Django shell by running the following command in the terminal:
Python manage.py shell
Enter following commands:
>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create(
title="title1",
description="description1").save()
>>> GeeksModel.objects.create(
title="title2",
description="description2").save()
>>> GeeksModel.objects.create(
title="title3",
description="description3").save()
With the backend setup complete, verify that instances have been created by visiting: http://localhost:8000/admin/geeks/geeksmodel/

For a Detail View, a unique identifier is required to retrieve a specific model instance, usually the primary key (id). This identifier needs to be defined in urls.py.
Go to geeks/urls.py and add the corresponding URL pattern.
from django.urls import path
# importing views from views.py
from .views import detail_view
urlpatterns = [
path('<id>', detail_view ),
]
Create a view and template to handle this functionality. In geeks/views.py:
from django.shortcuts import render
# relative import of forms
from .models import GeeksModel
# pass id attribute from urls
def detail_view(request, id):
# dictionary for initial data with field names as keys
context ={}
# add the dictionary during initialization
context["data"] = GeeksModel.objects.get(id = id)
return render(request, "detail_view.html", context)
Create a template in templates/detail_view.html:
<div class="main">
<!-- Specify fields to be displayed -->
{{ data.title }}<br/>
{{ data.description }}<br/>
</div>
Visit http://localhost:8000/1 to view the details of the model instance with ID 1.

The Detail View is now working correctly. Specific fields can also be displayed based on the required usage in different forms. Often, instead of using the id, a slug is used to define the Detail View for better readability and SEO-friendly URLs.
What is the purpose of a “Detail View” in a Django application?
-
A
To display a form for creating a new record
-
B
To delete all records of a model
-
C
To show a list of all objects of a model
-
D
To display data for exactly one object (record) from a model
A Detail View picks a single record (e.g. by primary key) and returns a response showing that object’s details.
In a function-based Detail View the view receives a request and an object identifier (like pk). What happens if no matching object exists for that identifier?
-
A
View returns an empty response
-
B
Django raises a DoesNotExist or 404 error
-
C
View returns the first object in the table
-
D
Django creates a new object automatically
When lookup fails for a requested object, Django must signal that record was not found — commonly by raising a 404 response or DoesNotExist.
Suppose the URL pattern for detail view is defined as path('<int:pk>/', views.detail_view, name='detail'). What does <int:pk> do?
-
A
It captures the object’s integer primary key from URL and passes it as
pkparameter to the view -
B
It forces pk to be string type
-
C
It hides the pk from the URL
-
D
It validates that pk does not exist in database
The path converter <int:pk> extracts an integer value from URL and forwards it to the view so the view can fetch the correct object.