How it works...

When you look at the results in the browser, you will see the pagination controls, similar to the following, is the list of movies:

How do we achieve this? When QuerySet is filtered out, we will create a paginator object passing QuerySet and the maximal amount of items that we want to show per page, which is 15 here. Then, we will read the current page number from the query parameter, page. The next step is to retrieve the current page object from paginator. If the page number is not an integer, we get the first page. If the number exceeds the amount of possible pages, the last page is retrieved. The page object has methods and attributes necessary for the pagination widget shown in the preceding screenshot. Also, the page object acts like QuerySet so that we can iterate through it and get the items from the fraction of the page.

The snippet marked in the template creates a pagination widget with the markup for the Bootstrap 3 frontend framework. We show the pagination controls only if there are more pages than the current one. We have the links to the previous and next pages, and the list of all page numbers in the widget. The current page number is marked as active. To generate URLs for the links, we use the {% modify_query %} template tag, which will be described later in the Creating a template tag to modify request query parameters recipe in Chapter 5, Custom Template Filters and Tags.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.240.142