![]() |
VOOZH | about |
A Detail View is used to show information about one specific record, such as a single post, product, or user profile.
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-instancesFor 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-demo1The 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.