Created
July 26, 2012 21:57
-
-
Save swilcox/3184840 to your computer and use it in GitHub Desktop.
a really crap set of template filters for django pagination rather than subclassing and mod'ing it
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
""" | |
Usage info: | |
Slap this file in a templatetags subdirectory off one of your apps: | |
In a template: | |
{% load crappy_page_filters %} | |
Then... given a situation like you were dealing with Haystack (and Twitter Boostrap style CSS) | |
you could do something like this in your template to provide page number links and info: | |
<div class="pagination"> | |
<ul> | |
<li class="prev {% if not page.has_previous %}disabled{% endif %}"><a href="?q={{ query }}&page={% if page.has_previous %}{{ page.previous_page_number }}{% else %}{{ page.number }}{% endif %}">« Previous</a></li> | |
{% for p in page.paginator.page_range|crappy_pages:page.number %} | |
<li class="{% if p == page.number %}active{% endif %} {% if "..." in p %}disabled{% endif %}"><a href="?q={{ query }}&page={{ p|crappy_pages_link:page.number }}">{{ p|crappy_pages_elli|safe }}</a></li> | |
{% endfor %} | |
<li class="next {% if not page.has_next %}disabled{% endif %}"><a href="?q={{ query }}&page={% if page.has_next %}{{ page.next_page_number }}{% else %}{{ page.number }}{% endif %}">Next »</a></li> | |
</ul> | |
</div> | |
Here the page context variable is a specific paginator page... Haystack style. | |
Not sure why I did this other than I didn't want to take in someone else's but yet I was in a hurry... | |
""" | |
from django import template | |
register = template.Library() | |
@register.filter | |
def crappy_pages(value,current_page,max_pages=8): | |
""" | |
4 scenarios: | |
1 - the number of pages is less than the max... just return the range untouched | |
2 - the current page is within the max number, so we're going to return the range starting at page 1 and add ... | |
3 - the current page is not within max_pages of the beginning, but within max_pages of the end so prepend ... | |
4 - the current page is in the middle of a bunch of page numbers... | |
""" | |
if value is not None: | |
page_range = value[:] | |
else: | |
page_range = [] | |
if len(page_range) > max_pages: | |
if current_page - max_pages < 0: | |
new_range = page_range[0:max_pages] | |
new_range.extend(['...',page_range[-1]]) | |
elif current_page + max_pages > len(page_range): | |
new_range = ['1','1...'] | |
new_range.extend(page_range[-max_pages:]) | |
else: | |
new_range = ['1','1...'] | |
new_range.extend(page_range[ (current_page - (max_pages / 2)):(current_page + (max_pages / 2))]) | |
new_range.extend(['...',page_range[-1]]) | |
return new_range | |
else: | |
return page_range | |
@register.filter | |
def crappy_pages_elli(value): | |
if value == '...' or value == '1...': | |
return '…' | |
return value | |
@register.filter | |
def crappy_pages_link(value,default=''): | |
if value == '...': | |
return default | |
elif value == '1...': | |
return default | |
else: | |
return value |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment