Python Django Purge Pagination
In Django, we will talk about purge-pagination, the application we can use for paging. Because Django’s paging structure does not work for us, we can use this paging application. In general, the number of pages in places where all pages are shown rather than a few pages from the beginning of the current page and a few pages from the end to create a beautiful image by showing. Purge pagination lets us handle this.
How to Install Django Purge Pagination
1 | pip install django-pure-pagination |
After installing the application, we need to add it to our project. Therefore;
1 2 3 4 | INSTALLED_APPS = ( ... 'pure_pagination', ) |
After you install the application settings.py we can make adjustments in it.
1 2 3 4 | PAGINATION_SETTINGS = { 'PAGE_RANGE_DISPLAYED': 10, 'MARGIN_PAGES_DISPLAYED': 2, } |
The PAGE_RANGE_DISPLAYED setting here specifies how many pages are displayed in the middle. If MARGIN_PAGES_DISPLAYED shows how many pages it will be at first and then.
So paging is primarily 2 pages…in the middle 10 pages. the probe will be two pages.
We add the application, so we can use it. Django’s own pagination structure is almost identical. In Django, we walk the pagination methods.
1 | from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger |
In Purge pagination, only the application is changing. The methods and their use are the same.
1 | from pure_pagination import Paginator, EmptyPage, PageNotAnInteger |
We can use it in view like this.
1 2 3 | tum_yazilar = Yazilar.objects.filter(onay=1) yazi_sayfalari = Paginator(tum_yazilar, 10, request=request) yazilar = yazi_sayfalari.page(page) |
Finally, we can show the pages in the template as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | {% if yazilar.has_previous or yazilar.has_next %} {% load i18n %} <ul class="pagination"> {% if yazilar.has_previous %} <li><a href="?{{ yazilar.previous_page_number.querystring }}" class="prev">‹‹</a></li> {% else %} <li><span class="disabled prev">‹‹ </span></li> {% endif %} {% for page in yazilar.pages %} {% if page %} {% ifequal page yazilar.number %} <li class="active"><span>{{ page }}</span></li> {% else %} <li><a href="?{{ page.querystring }}" class="page">{{ page }}</a></li> {% endifequal %} {% else %} <li><span>...</span></li> {% endif %} {% endfor %} {% if yazilar.has_next %} <li><a href="?{{ yazilar.next_page_number.querystring }}" class="next"> ››</a></li> {% else %} <li><span class="disabled next">››</span></li> {% endif %} </ul> {% endif %} |
Now our pagination will look like this. The style changes according to your theme. But this is how the number of pages will be. We got it from the official app’s github Account.