How to Create a List View with Python in Django 


               


How to Create a List View with Python in Django



Python


In this article, we show how to create a list view with Python in Django.

A list view is simply a list of all objects, such as all objects of a database table.

So all the objects are simply listed out in a list view.

List views are very important because they are very useful for listing out all items such as from a database table. For example, let's say we have a database table of movies. In a list views, a the movies will be listed out, so that a user can view all of them.

So now that you know what a list view is, how do we create one?

First, let's create a database table and populate it.

Then based off of this data from the database table, we'll show how to create a list view of all of the objects of the database table.

So below we create a database table called Movie that has 3 fields, title, genre, and year it was released.

This is shown in the code below.



So we've created a database table called Movie.

There are 3 columns: name, genre, and year_released.

We then create an __str__ function just so that a generic object isn't returned when we call an object.

Make sure that you makemigrations and migrate in order to have the saved changes to the models.py file reflect across the entire project.

So then we go into the views.py file and we import this database table, Movie, from the models.py file and we create a variable that contains all of the objects of the Movie database table. We can then pass this variable into the context dictionary and render it into template.

This is shown in the code below.



So in the views.py file, we import render, since we're going to render a template file.

We import the database, Movie, that we created.

We then create our function-based view, movies_list_view()

Inside of this function, we create a variable, all movies, which contains all objects of the Movie (database table).

We then put this variable, allmovies, in the context dictionary.

And then render this variable to the movies-list-view.html template file.


template file

Lastly, we create our list view in the template file, in this case, movies-list-view.html

How you create your list view is very variable, depending on how you want it.

A list view is simply a list of all of the objects of database table (or other data type).

You can display this in an unordered list, an ordered list, simply with break tags in between them, in a table (in rows), etc. There are many ways of displaying the objects.

In this code, we'll simply separate the data with HTML break tags in between.



So we simply create a list view with HTML break tags separating the different objects of the database table.

First, we have our HTML tag and the head tag.

The body contains the list view.

We first have the h1 tag, List of Movies.

We then have a for loop that loops through all movies in the Movie database talbe.

We print out {{ movie.title }} which prints out the title field of the database table for each movie.

I populated the movie database table with 3 movies, Home Alone 1, Home Alone 2, and Christmas Vacation.

Running the above code, I get the following output, shown below.

List view in Django



And this is all that is required to create a list view with Python in Django.


Related Resources

How to Create a Video Uploader with Python in Django

How to Create an Image Uploader with Python in Django



HTML Comment Box is loading comments...