How to Create a Generic DetailView in Django



Python


In this article, we show how to create a generic DetailView in Django.

A generic DetailView is a class-based view in Django instead of using something else, such as function-based view.

So in this article, we take the approach of showing you how to create a detail view from a generic class-based DetailView.

It turns out that it's very simple to do.

It's probably even easier than a function-based view.

So we'll go through each file, such as the models.py file, the views.py file, the urls.py file, and the template file, to show how to create a list view from a generic DetailView in Django.


models.py File

So, just to show you which model we're working with, we show the model below.

This is going to be a simple model that contains 3 fields. These 3 fields are title, description, and price.

This is shown in the code below.



So this is the model.

Now we move onto the views.py File.


views.py File

The views.py file for a generic DetailView is very easy and requires even less code than a function-based view.

The code for the views.py file is shown below.



So in order to create a generic DetailView in DJango, we must import DetailView from django.views.generic.

And obviously we have to import the model we created in the models.py file.

Since we are doing a class-based view, instead of a function-based view, we use the keyword class to define the view (rather than def with function-based views).

By convention, you call the class-based view the model name, followed by DetailView. So in this case, our class-based view is called, ProductDetailView.

And the only thing we have to define is the model, which is Product, in this case.

With class-based views, we don't have to pass in a context dictionary or the template to render, because it's all done for us.

We have to create the template, but we don't have to tell which template to render, because, by default, a generic class-based view assumes the template will be the model name followed by, _detail. In this case, the default template to create is product_detail.html. The full path is, /products/product_detail.html, in the templates folder in the app.

You can change this default template, by typing in, template_name= 'products/detail.html', for example, if you want to change the template file path or name.

If you want to add a context variable to the template page, you can do so, but usually this is not necessary.

So now we go onto the urls.py file.


urls.py File

So within the products app, we have to create a urls.py file in order to render out the generic DetailView.

Below is how our urls.py file will look.



So class-based views differ from function-based views, in that you must add, as_view() to the ending of the class-based view in order to render it.

This is really the only difference. If you don't add this, an error will be created.


Template File

Now to the last page, which is our template file.

This is the page that shows the detail information about an object in the database.



So on this detail view, we show the title, price, and image of the product.

By default, with a generic DetailView, the variable to refer to an object is, object.

To get the title of the object, we use the statement, {{ object.title }}

To get the price of the object, we use the statement, {{ object.price }}

To get the full path to an image, we use the statement, {{ object.image.url }}

And this is all that is needed to create a generic DetailView in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...