Last active
February 3, 2022 11:30
-
-
Save sumitlni/4f308e5999d2d4d8cb284fea7bf0309c to your computer and use it in GitHub Desktop.
Code for Proper Pagination Article (https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432) on Medium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if is_paginated %} | |
{% load proper_paginate %} | |
{% load url_replace %} | |
<ul class="pagination"> | |
{% if page_obj.number == 1 %} | |
<li class="disabled"><span>⇤</span></li> | |
{% else %} | |
<li><a class="page-link" href="?{% url_replace request 'page' 1 %}">⇤</a></li> | |
{% endif %} | |
{% if page_obj.has_previous %} | |
<li><a class="page-link" href="?{% url_replace request 'page' page_obj.previous_page_number %}">«</a></li> | |
{% else %} | |
<li class="disabled"><span>«</span></li> | |
{% endif %} | |
{% for i in paginator|proper_paginate:page_obj.number %} | |
{% if page_obj.number == i %} | |
<li class="active"><span>{{ i }} <span class="sr-only">(current)</span></span></li> | |
{% else %} | |
<li><a class="page-link" href="?{% url_replace request 'page' i %}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
{% if page_obj.has_next %} | |
<li><a class="page-link" href="?{% url_replace request 'page' page_obj.next_page_number %}">»</a></li> | |
{% else %} | |
<li class="disabled"><span>»</span></li> | |
{% endif %} | |
{% if page_obj.number == paginator.num_pages %} | |
<li class="disabled"><span>⇥</span></li> | |
{% else %} | |
<li><a class="page-link" href="?{% url_replace request 'page' paginator.num_pages %}">⇥</a></li> | |
{% endif %} | |
</ul> | |
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
register = template.Library() | |
@register.filter(name='proper_paginate') | |
def proper_paginate(paginator, current_page, neighbors=10): | |
if paginator.num_pages > 2*neighbors: | |
start_index = max(1, current_page-neighbors) | |
end_index = min(paginator.num_pages, current_page + neighbors) | |
if end_index < start_index + 2*neighbors: | |
end_index = start_index + 2*neighbors | |
elif start_index > end_index - 2*neighbors: | |
start_index = end_index - 2*neighbors | |
if start_index < 1: | |
end_index -= start_index | |
start_index = 1 | |
elif end_index > paginator.num_pages: | |
start_index -= (end_index-paginator.num_pages) | |
end_index = paginator.num_pages | |
page_list = [f for f in range(start_index, end_index+1)] | |
return page_list[:(2*neighbors + 1)] | |
return paginator.page_range |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import template | |
register = template.Library() | |
@register.simple_tag | |
def url_replace(request, field, value): | |
query_string = request.GET.copy() | |
query_string[field] = value | |
return query_string.urlencode() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment