Skip to content

Instantly share code, notes, and snippets.

@oscarmcm
Created April 27, 2015 03:25
Show Gist options
  • Save oscarmcm/36ba8b495de5b9747341 to your computer and use it in GitHub Desktop.
Save oscarmcm/36ba8b495de5b9747341 to your computer and use it in GitHub Desktop.
A Django template tag for pagination
{% load i18n %}
<div class="row">
<div class="col-xs-12">
<section class="paginator text-center">
<nav>
<ul class="pagination">
<li>
{% if page.has_previous %}
<a href="?page={{ page.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true"><i class="icon icon-chevron-left"> Anterior</i></span>
</a>
{% endif %}
</li>
{% for page_num in begin %}
{% ifequal page.number page_num %}
<li class="active">
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% else %}
<li>
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% endifequal %}
{% endfor %}
{% if middle %}
<li>
<a>...</a>
</li>
{% for page_num in middle %}
{% ifequal page.number page_num %}
<li class="active">
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% else %}
<li>
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% endifequal %}
{% endfor %}
{% endif %}
{% if end %}
<li>
<a>...</a>
</li>
{% for page_num in end %}
{% ifequal page.number page_num %}
<li class="active">
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% else %}
<li>
<a href="?page={{ page_num }}">{{ page_num }}</a>
</li>
{% endifequal %}
{% endfor %}
{% endif %}
<li>
{% if page.has_next %}
<a href="?page={{ page.next_page_number }}" aria-label="Next">
<span aria-hidden="true">Siguiente <i class="icon icon-chevron-right"></i></span>
</a>
{% endif %}
</li>
</ul>
</nav>
</section>
</div>
</div>
from django import template
from django.template import loader
register = template.Library()
@register.inclusion_tag('pagination.html')
def end_pagination(page, begin_pages=2, end_pages=2, before_current_pages=4, after_current_pages=4):
"""
return google like pagination
Usage::
{% load end_tags %}
{% end_pagination the_obj_to_paginate %}
Example::
{% end_pagination page_obj %}
At this case page_obj is the defaul
object for pages that django provide
"""
before = max(page.number - before_current_pages - 1, 0)
after = page.number + after_current_pages
begin = page.paginator.page_range[:begin_pages]
middle = page.paginator.page_range[before:after]
end = page.paginator.page_range[-end_pages:]
last_page_number = end[-1]
def collides(firstlist, secondlist):
"""
Returns true if lists collides (have same entries)
Example::
>>> collides([1,2,3,4],[3,4,5,6,7])
True
>>> collides([1,2,3,4],[5,6,7])
False
"""
return any(item in secondlist for item in firstlist)
# If middle and end has same entries, then end is what we want
if collides(middle, end):
end = range(max(last_page_number - before_current_pages - after_current_pages, 1), last_page_number + 1) #noqa
middle = []
# If begin and middle ranges has same entries, then begin is what we want
if collides(begin, middle):
begin = range(1, min(before_current_pages + after_current_pages, last_page_number) + 1) #noqa
middle = []
# If begin and end has the same entries then begin is what we want
if collides(begin, end):
begin = range(1, last_page_number + 1)
end = []
return {
'page': page,
'begin': begin,
'middle': middle,
'end': end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment