- Author:
- goodsanket
- Posted:
- January 22, 2010
- Language:
- Python
- Version:
- 1.1
- Score:
- 0 (after 0 ratings)
This is very simple Paginator function built over Django's Paginator. Just pass following:- 1. request object 2. object list - This is a list of object you want to paginate 3. per page - how many items you need per_page 4. paginator_range - Specify how many links you want on either side of current page link.
Refer to Paginator reference here
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 27 28 29 30 31 32 33 34 35 36 37 38 39 | def get_paginators(request, objects, per_page=20, paginator_range=5):
paginator_range = int(paginator_range)
from django.core.paginator import Paginator, InvalidPage, EmptyPage
paginator = Paginator(objects, per_page)
# Make sure page request is an int. If not, deliver first page.
try:
page = int(request.GET.get('page', '1'))
except ValueError:
page = 1
# If page request (9999) is out of range, deliver last page of results.
try:
obj_list = paginator.page(page)
except (EmptyPage, InvalidPage):
obj_list = paginator.page(paginator.num_pages)
if obj_list.number-paginator_range >= 0:
from_ = obj_list.number-paginator_range
else:
from_ = 0
if obj_list.number+paginator_range <= len(paginator.page_range):
to_ = obj_list.number+paginator_range
else:
to_ = len(paginator.page_range)
page_range = paginator.page_range[from_:to_]
return (obj_list, page_range)
# Use follwing code in your template to show page_range
{% for item in page_range %}
{% ifequal item event_list.number %}
<span class="current number">{{item}}</span>
{% else %}
<a href="?page={{item}}" class="number">{{item}}</a>
{% endifequal %}
{% endfor %}
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.