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.