How to Create a Generic ListView in Django



Python


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

A generic ListView 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 list view from a generic class-based ListView.

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 ListView in Django.


models.py File

So the first file we have to do is create our model.

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 you then have to just make sure you run the commands, py manage.py makemigrations, and then the command, py manage.py migrate.

Now we move onto the views.py File.


views.py File

The views.py file for a generic ListView 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 ListView in DJango, we must import ListView 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 ListView. So in this case, our class-based view is called, ProductListView.

And the only thing we have to define is the queryset, which represents all of the objects from the Product model.

So, queryset= Product.objects.all()

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, _list. In this case, the default template to create is product_list.html. The full path is, /products/product_list.html, in the templates folder in the app.

You can change this default template, by typing in, template_name= 'products/list.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 ListView.

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 all of the items in the database table that we created.



So on this list view, all we do is list out the title and price for each products from our Product model.

By default, with a generic ListView, all of the objects in the variable, object_list.

We use a for loop to loop through all objects in the object_list.

We output the title and price of each object in the database.

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


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...