Django pagination - How to limit the pages? -
i'm using code display pagination on page
<ul class="pagination pagination-sm"> {% if restaurants.has_previous %} <li><a href="?page={{ restaurants.previous_page_number }}">prev</a></li> {% endif %} {% page in restaurants.paginator.page_range %} <li class="{% if restaurants.number == page %}active{% endif %}"><a href="?page={{ page }}">{{ page }}</a></li> {% endfor %} {% if restaurants.has_next %} <li><a href="?page={{ restaurants.next_page_number }}">next</a></li> {% endif %} </ul>
the issue code above shows page this:
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | , on..
instead show this:
1 | 2 | 3 | 4 | 5
how can that?
this view:
def listing(request): list_restaurants = shopaccount.objects.filter(is_active=1) # active products paginator = paginator(list_restaurants, 20) # show 20 products per page page = request.get.get('page') try: restaurants = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. restaurants = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. restaurants = paginator.page(paginator.num_pages) return render_to_response('restaurants/list.html', {'restaurants': restaurants}, context_instance=requestcontext(request))
i use :
<li {% ifequal page current %}class="active"{% endifequal %}> <a href={% ifequal page -4 %} "/blog/1/"><< {% else %}{% ifequal page -3 %} "/blog/{{ current | add:"-1" }}/">< {% else %}{% ifequal page -2 %} "/blog/{{ current | add:"1" }}/">> {% else %}{% ifequal page -1 %} "/blog/{{ maximum }}/">>> {% else %}{% ifequal page 0 %} "#">... {% else %}{% ifequal page current %} "#" class="page_link">{{ page }} {% else %} "/blog/{{ page }}/">{{ page }} {% endifequal %} {% endifequal %} {% endifequal %} {% endifequal %} {% endifequal %} {% endifequal %} </a> </li>
and these views :
def makepaginator(current, maximum): """makepaginator() returns set of number reprensents buttons. -4 means first page. -3 means previous page. -2 means next page. -1 means last page. 0 means dot. others means number of target page.""" nb_pages_left_left = 3 nb_pages_right_right = nb_pages_left_left nb_pages_center_right = 3 nb_pages_center_left = nb_pages_center_right = 2 current = int(current) if current > 1: p = [-4, -3] else: p = [] p.append(1) while <= maximum: if (i > nb_pages_left_left , < maximum - nb_pages_right_right , (i - current > nb_pages_center_right or current - > nb_pages_center_right + 1)): p.append(0) while (i > nb_pages_left_left , < maximum - nb_pages_right_right , (i - current > nb_pages_center_right or current - > nb_pages_center_left + 1)): += 1 else: p.append(i) += 1 if current < maximum: p.append(-2) p.append(-1) return (p) def news(request, page_num=1): paginator = paginator(article.objects.all().order_by('-date'), settings.articles_per_page) try : page = paginator.page(page_num) print(type(page_num)) return (render(request, "main/blog.html", {"paginator" : paginator, "page_num" : int(page_num), "page" : page, "buttons" : makepaginator(page_num, paginator.num_pages)})) except (pagenotaninteger, emptypage) e: return (notfound(request))
with bootstrap :
it may dirty works website. can try it. if have improvements suggests, glad though. :)
Comments
Post a Comment