Paginating Storage Views
In this video, we will learn how to paginate Storage List view of vaccine app. We have already disccused how to paginate function based view and class based view. Today we will be learning how to paginate any generic ListView.
Views
At first, open views.py file of center and then navigate to the Storage List view. In this view, at first we need to set the ordering of our queryset.
ordering = ["id"]
By specifying ordering = id, we are telling django to order the queryset for Storage by its id.
Now, to paginate the generic List view, just write,
paginate_by = 2
This will paginate the queryset by 2 objects per page.
Templates
After that, open storage-list template and then, We will iterate over page_obj in the for loop.
{% for storage in page_obj %}
At last, we will include the pagination component in the templates for including the first page link, last page link and other pages link.
{% include "components/pagination.html" %}
[Run the development server and see the changes.]
[Commit the changes and push it in the remote repository.]