DetailView - Class Based Views Django
Last Updated :
19 Nov, 2025
A DetailView is a built-in class-based view used to show a single record from the database. It automatically fetches the record using a primary key or slug, reducing extra code.
- Specify the model from which the record will be retrieved.
- Set the template that will display the record’s details.
- The selected record is available in the template as an object or a custom context name.
- Django handles the lookup and passes the data to the template automatically.
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:
Python
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 with their title name
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
Now let's create some instances of this model using shell, run form bash.
Python manage.py shell
Enter following commands:
>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create( title="title1", description="description1")
>>> GeeksModel.objects.create(title="title2", description="description2")
>>> GeeksModel.objects.create(title="title3", description="description3")
Now that the backend setup complete, we need to confirm that instances have been created by visiting: http://localhost:8000/admin/geeks/geeksmodel/ 
To create a DetailView, it is only necessary to specify the model. Django’s DetailView will then look for a template named app_name/modelname_detail.html. Here, expected template path is geeks/templates/geeks/geeksmodel_detail.html.
In geeks/views.py:
Python
from django.views.generic.detail import DetailView
from .models import GeeksModel
class GeeksDetailView(DetailView):
# specify the model to use
model = GeeksModel
Now, create a url path to map the view in geeks/urls.py:
Python
from django.urls import path
from .views import GeeksDetailView
urlpatterns = [
# <pk> is identification for id field, slug can also be used
path('<pk>/', GeeksDetailView.as_view()),
]
Next, create a template in templates/geeks/geeksmodel_detail.html:
html
<h1>{{ object.title }}</h1>
<p>{{ object.description }}</p>
Now, Visit http://localhost:8000/1/ to view the details of the model instance with ID 1.
Manipulate Context Data in DetailView
By default DetailView will only display fields of a table. If one wants to modify this context data before sending it to template or add some extra field, context data can be overridden.
In geeks/views.py:
Python
from django.views.generic.detail import DetailView
from .models import GeeksModel
class GeeksDetailView(DetailView):
# specify the model to use
model = GeeksModel
# override context data
def get_context_data(self, *args, **kwargs):
context = super(GeeksDetailView,self).get_context_data(*args, **kwargs)
# add extra field
context["category"] = "MISC"
return context
In geeks/templates/geeksmodel_detail.html:
html
<h1>{{ object.title }}</h1>
<p>{{ object.description }}</p>
<p>{{ category }}</p>
Now, Visit http://localhost:8000/1/ to view the details:
What is the main purpose of Django’s DetailView generic class-based view?
-
To create a new database record
-
To list all records from a model
-
To display a single record (object) from a model with full details
-
Explanation:
DetailView automatically fetches an instance (by primary key or slug) and sends that object to template for detailed display.
When using DetailView, how does Django determine which database record to fetch?
-
It fetches the first record always
-
It fetches a random record
-
It fetches all records and then picks based on template logic
-
It uses primary key or slug from URL parameters
Explanation:
DetailView expects a unique identifier (like pk or slug) passed via URL to identify which object to retrieve.
In a template rendered by DetailView what is the default context variable name used for the object?
Explanation:
DetailView makes the retrieved object available in template context by default under the name “object”.
Quiz Completed Successfully
Your Score : 2/3
Accuracy : 0%
Login to View Explanation
1/3
1/3
< Previous
Next >
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice