VOOZH about

URL: https://www.geeksforgeeks.org/python/detail-view-function-based-views-django/

⇱ Detail View - Function based Views Django - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Detail View - Function based Views Django

Last Updated : 18 Nov, 2025

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

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/ 

👁 django-Detailview-check-models-instances

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.

Create a view and template to handle this functionality. In geeks/views.py:

Create a template in templates/detail_view.html:

Visit http://localhost:8000/1 to view the details of the model instance with ID 1.

👁 django-detail-view-demo1

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.

Comment
Article Tags: