Skip to content

Storage List

In this video, we are going to learn about generic List View.

Generic ListView is a class-based view that is used to display a list of objects retrieved from a database. It provides a pre-built implementation that can handle common tasks related to displaying a list of objects, such as fetching the objects, generating the context data, and rendering a template to present the list to the user.

In this video, We will create a List View api for Storage model.

Resources:

Setup

First of all, download the Section 9 Project Code files from the resources section.

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Then, migrate the changes to the SQLite database.

  • Now, create a superuser having username "admin" and password "abde@12345".

Now, we are ready to go. At first, we need to create vaccine, center and storage data from the admin panel.

Vaccine 1:
    name = Covishield

Vaccine 2:
    name = Polio

Center 1:
    name = ABC Hospital

Center 2:
    name = XYZ Hospital

Storage 1:
    center 1 - ABC Hospital
    vaccine 1 - Covishield

Storage 2:
    center 2 - XYZ Hospital
    vaccine 2 - Polio

View

Now, lets start creating Storage List API using generic views. First of all, open views.py file of center.

At first, we need to import Storage model

from center.models import Center, Storage

Now, we need to import generic view from django.

from django.views import generic

After that, lets create the Storage List API. Create a class named Storage List and inherit generic.ListView.

class StorageList(generic.ListView):

This is how, you can create generic ListView in django.

The first thing which the this generic ListView takes is queryset.

    queryset = Storage.objects.all()

This class will use this queryset in order to fetch the storage list from the database.

Once, the storage list is fetched from the database, then django needs a template to render that storage list.

    template_name = "storage/storage-list.html"

Template

Now, create a folder named "storage" in templates and create storage-list.html

In the html file, we will iterate over storage list and render the storage information

{% for storage in object_list %} 
    {{ storage.center.name }}
    {{ storage.vaccine.name }}
{% endfor %}

The Storage List will provide context data to this template and the storage list data will be accessible via object_list name. You can also change the name of object_list to storage_list.

URL

Now, lets create a url pattern for this view. Open your urls.py file of center and add new path.

urlpatterns = [
    path("storage/", views.StorageList.as_view(), name="storage-list"),
]

Now, run the development server and visit this url.

Template Modification

Now, lets modify the storage list template and make it look better.

I have attached storage-list.html file in the resources section. Download it, copy its content and paste it in the html file.

{% extends 'mysite/base.html' %} 

{% block title %} 
    <title>Storage List</title>
{% endblock title %} 

{% block content %}
<div class="mt-3">

  <div class="d-flex align-items-center justify-content-between">
    <h3 class="text-center">
      <i class="fas fa-box"></i> Vaccine Storage List
    </h3>
    <a href="#"><i class="fas fa-plus"></i> Create Storage</a>
  </div>

  <table class="table table-responsive table-hover text-center">
    <thead>
      <tr>
        <th scope="col">Center Name</th>
        <th scope="col">Vaccine Name</th>
        <th scope="col">Total Quantity</th>
        <th scope="col">Booked Quantity</th>
        <th scope="col">Select</th>
      </tr>
    </thead>

    <tbody>
      {% for storage in object_list %}
      <tr>
        <td>{{ storage.center.name }}</td>
        <td>{{ storage.vaccine.name }}</td>
        <td>{{ storage.total_quantity }}</td>
        <td>{{ storage.booked_quantity }}</td>
        <td><a href="#" class="btn-sm btn-outline-primary">View Details</a></td>
      </tr>
      {% endfor %}
    </tbody>

  </table>
</div>
{% endblock content %}

Now, our storage list page looks much more better.

View Modification

If you observe our storage list page, you can notice that, this page lists all the storage stored in the database. But I dont want to see all the storage list at one place. I want to view the storage list filtered by their center and For that I need to apply filter on my queryset in the Storage List.

Now, here the biggest problem is how do we get the center id. How do we know which center storage we have to list.

For this problem, what we will do, we will take center id in the url. So, open your urls.py file of center.

path("<int:center_id>/storage/", views.StorageList.as_view(), name="storage-list"),

Add center_id before /storage. In this way, we will know the center id and we can filter the storage for that particular center.

Now, in the view, we need to apply filter on our queryset to filter storage for that particular center only.

For that, we need to override the get_queryset method.

def get_queryset(self):

In this method, at first, we will return the queryset which we have written here.

return super().get_queryset()

Now, we will apply filter here.

.filter(center_id=self.kwargs["center_id"])

Using self.kwargs, we can read center_id data from the url.

Now, open your center-detail page and add the url which we have created in this html file.

<button class="btn btn-sm btn-dark m-1">
    <a href="{% url 'center:storage-list' center.id %}">
        <i class="fas fa-eye"></i> View Storage
    </a>
</button>

[Run the development server and see the changes.]

In this way, we have learnt how to handle list page for a given model using generic List View.