Last modified: May 30, 2023 By Alexander Williams

Understanding Django DetailView With Simple Example

In this tutorial, we will explore the practical application of Django's DetailView by demonstrating its usage through a simple example, allowing us to grasp its functionality and leverage its capabilities effectively.

1. What is DetailView?

DetailView in Django is a class-based view that enables us to present detailed information extracted from a registration model effortlessly. It simplifies the process of displaying specific data associated with a single object.

To better understand its functionality, let's dive into an illustrative example.

2. Django DetailView Example

In this example, our goal is to display the records' details dynamically. We will begin by creating the necessary model in Django to achieve this. The model will define the structure and fields of our records. Let's start by creating the model.

models.py

Let's create a simple model that will allow us to create a dynamic page for each record.

# models.py
class Users(models.Model):
    firstname = models.CharField(max_length=300)
    last_name = models.CharField(max_length=300)

In this example, we have created a Users model with two fields: first_name and last_name. These fields represent the first name and last name of each user record.

views.py

We need to add the following code to our views to implement the desired functionality.

#views.py

from django.views.generic import DetailView

class UsersDetail(DetailView):
    model = Users # model
    template_name = 'test.html' # template's path
    slug_field = 'firstname' # slug field    

We import the class from Django's generic views module in the code snippet above. Then, we create a UsersDetail class that inherits from DetailView. Inside the class, we specify the model attribute as Users, which corresponds to the model we created earlier.

The template_name attribute represents the path to the template that will be used to render the detail view. Additionally, we set the slug_field attribute to first_name, which determines the field in the model used as part of the URL.

Make sure to adjust the template_name value according to the actual path of your template file.

urls.py

Now, let's add a path for our UsersDetail view in the urls.py file.

from django.urls import path
from .views import UsersDetail

urlpatterns = [
    path('<slug:slug>/', UsersDetail.as_view(), name='user-detail'),
]

In this code snippet, we define a path with a slug parameter (<slug:slug>/) that will match the URL for each user detail page.

test.html

Now, let's create the test.html file in the TEMPLATES folder and add the following code:


#test.html
<!DOCTYPE html>
<html>
<head>
<title>Detailview</title>
</head>
<body>
<h1>hello {{object.firstname}} !</h1>
</body>
</html>

In the test.html template, we have a basic structure with an <h1> tag for the page heading. We then access the first_name and last_name attributes of the object in double curly braces {{ }}. This will display the first name and last name of the user record being viewed.

Now, Let's add some records to our model.

 

 

Finally, here is the result of our view:

Conclusion

In conclusion, we have covered the implementation of Django's DetailView through a simple example. The example demonstrates the power and simplicity of Django's DetailView in handling dynamic data rendering.