- Author:
- agusmakmun
- Posted:
- July 14, 2016
- Language:
- Python
- Version:
- 1.7
- Score:
- 3 (after 3 ratings)
Dynamic Paginator Mixin for Django 1.8. - 1.9., also work for CBV (Class Bassed View) but not for "django generic view".
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | from django.core.paginator import (Paginator, EmptyPage, PageNotAnInteger)
class PaginatorMixin(object):
def __init__(self, queryset, numb_pages, request_page):
self.queryset = queryset #egg: models.Post.objects.all()
self.numb_pages = numb_pages #egg: int 10 `is number per page`
self.request_page = request_page #egg: request.GET.get('page')
def page_numbering(self):
paginator = Paginator(self.queryset, self.numb_pages)
try:
pagination = paginator.page(self.request_page)
except PageNotAnInteger:
pagination = paginator.page(1)
except EmptyPage:
pagination = paginator.page(paginator.num_pages)
index = pagination.number - 1
limit = 5 #limit for show range left and right of number pages
max_index = len(paginator.page_range)
start_index = index - limit if index >= limit else 0
end_index = index + limit if index <= max_index - limit else max_index
page_range = list(paginator.page_range)[start_index:end_index]
return page_range
def queryset_paginated(self):
paginator = Paginator(self.queryset, self.numb_pages)
try:
queryset_paginated = paginator.page(self.request_page)
except PageNotAnInteger:
queryset_paginated = paginator.page(1)
except EmptyPage:
queryset_paginated = paginator.page(paginator.num_pages)
return queryset_paginated
"""Example:
# 1. your_app/views.py
def homepage(request):
posts = Post.objects.published().order_by('-id')
get_page = request.GET.get('page')
NUMB_PAGES = 10
page_obj = PaginatorMixin(posts, NUMB_PAGES, get_page).queryset_paginated()
page_range = PaginatorMixin(posts, NUMB_PAGES, get_page).page_numbering()
posts = page_obj.object_list
ctx = {
'posts': posts,
'page_obj' : page_obj,
'page_range' : page_range
}
return render(request, 'your_app/homepage.html', ctx)
# 2. your_app/templates/your_app/homepage.html
{% if posts %}
<ul class="pagination">
<li {% if not page_obj.has_previous %}class="disabled"{% endif %}>
<a {% if page_obj.has_previous %}href="?page={{ page_obj.previous_page_number }}" aria-label="Previous" {% endif %}><span aria-hidden="true">«</span></a>
</li>
<li><a href="?page=1">First</a></li>
{% for linkpage in page_range %}
{% ifequal linkpage page_obj.number %}
<li class="active">
<a>{{ page_obj.number }}<span class="sr-only">(current)</span></a>
</li>
{% else %}
<li><a href="?page={{ linkpage }}">{{ linkpage }}</a></li>
{% endifequal %}
{% endfor %}
<li><a href="?page={{ page_obj.paginator.num_pages }}">Last</a></li>
<li {% if not page_obj.has_next %}class="disabled"{% endif %}>
<a {% if page_obj.has_next %}href="?page={{ page_obj.next_page_number }}" aria-label="Next" {% endif %}><span aria-hidden="true">»</span></a>
</li>
</ul>
{% endif %}
"""
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 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
The spelling is "Dynamic" not "Dinamic"
#
@shayneo ups sorry.. typo.. :D
#
Please login first before commenting.