Last active
March 28, 2018 22:04
-
-
Save olivx/041e183bb1ecdf3854ddc5801e2f1469 to your computer and use it in GitHub Desktop.
create pagination
This file contains hidden or 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
<div class="text-center"> | |
{% if object_list.has_other_pages %} | |
<ul class="pagination"> | |
{% if object_list.has_previous %} | |
<li> | |
<a href="?page={{ object_list.previous_page_number }}{% querystring request %}">«</a> | |
</li> | |
{% else %} | |
<li class="disabled"> | |
<span>«</span> | |
</li> | |
{% endif %} | |
{% for pg in object_list.paginator.page_range %} | |
{% if pg in object_list.paginator.page_range|slice:":2" or pg in object_list.paginator.page_range|slice:"-2:" %} | |
<li class="{% if object_list.number == pg %}active{%endif%}"> | |
<a href="?page={{ pg }}{% querystring request %}">{{ pg }}</a> | |
</li> | |
{% else %} | |
{% if pg == object_list.paginator.page_range|first|add:"2" or pg == object_list.paginator.num_pages|add:'-2' %} | |
<li> | |
<span>...</span> | |
</li> | |
{% endif %} | |
{% if pg in object_list.paginator.page_range|slice:"2:-2" %} | |
{% if pg > object_list.number|add:"-4" and pg < object_list.number|add:"4"%} | |
<li class="{% if object_list.number == pg %}active{% endif %}"> | |
<a href="?page={{ pg }}{% querystring request %}"> | |
{{ pg }} | |
</a> | |
</li> | |
{% endif %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% if object_list.has_next %} | |
<li> | |
<a href="?page={{ object_list.next_page_number }}{% querystring request %}">»</a> | |
</li> | |
{% else %} | |
<li class="disabled"> | |
<span>»</span> | |
</li> | |
{% endif %} | |
</ul> | |
{% endif %} | |
</div> |
This file contains hidden or 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.core.paginator import Paginator, EmptyPage, PageNotAnInteger | |
def paginator(request, object_list, pages): | |
page = request.GET.get('page', 1) | |
paginator = Paginator(object_list, pages) | |
try: | |
object_list = paginator.page(page) | |
except PageNotAnInteger: | |
object_list = paginator.page(1) | |
except EmptyPage: | |
object_list = paginator.page(paginator.num_pages) | |
return object_list |
This file contains hidden or 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
@register.simple_tag | |
def querystring(request): | |
updated = request.GET.copy() | |
updated.pop('page','') | |
for key, value in request.GET.items(): | |
if not key == 'page': | |
updated[key] = value | |
if len(updated) == 0: | |
return '' | |
return '&{}'.format(updated.urlencode()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment