Django::: paginator show all page of objects, how can i fix it -
paginator shows: first prev 1 2 3 4 5 6 7 8 9 10 11 12 13 next last how can make « first‹ prev…2 3 4 5 6 7 8 9 10…next ›last »
i dont want use django-paginator package tried django paginator tag not sucess.
views.py
def hire(request): hire_article_list = hire_article.objects.all().order_by('-id')[0:200] paginator = paginator(hire_article_list, 2) # show 25 contacts per page page = request.get.get('page') try: hire_article_s = paginator.page(page) except pagenotaninteger: # if page not integer, deliver first page. hire_article_s = paginator.page(1) except emptypage: # if page out of range (e.g. 9999), deliver last page of results. hire_article_s = paginator.page(paginator.num_pages) #return render_to_response('hire/list.html', {"page_list": page_list}) context = {'hire_article_s': hire_article_s} return render(request, 'hire/list.html', context) list.html
{% load static staticfiles %} {% j in hire_article_s %} {# each "j" page_list model object. #} <li><a href="/hire/{{ j.slug }}-{{j.id}}">{{ j.hiring|capfirst}}</a></li> {% endfor %} <div class="pagination"> <span class="step-links"> {% if hire_article_s.has_previous %} <a href="?page=1">first</a> <a href="?page={{ hire_article_s.previous_page_number }}">prev</a> {% endif %} {% page in hire_article_s.paginator.page_range %} {% ifequal page page.number %} <!-- special page -->love <span id="currentpage">page</span> {% else %} <!-- other pages --> {% endifequal %} {% if page == hire_article_s.number %} {{ page }} {% else %} <a href="/hire/?page={{ page }}">{{ page }}</a> {% endif %} {% endfor %} {% if hire_article_s.has_next %} <a href="?page={{ hire_article_s.next_page_number }}">next</a> <a href="?page={{ hire_article_s.paginator.num_pages}}">last</a> {% endif %} </span> </div>
with current page 6, want show 2 3 4 5 [6] 7 8 9 10.
with current page 5, want show 1 2 3 4 [5] 6 7 8 9.
with current page 10, want show 6 7 8 9 [10] 11 12 13 14.
so, law? law show current page number, , 4 smaller pages + 4 larger pages.
given current number n, want show (n - 4) (n - 1), , (n + 1) (n + 4)
so, instead of doing:
{% page in hire_article_s.paginator.page_range %}
you can do:
{% page in hire_article_s.paginator.page_range|page_filter:hire_article_s.number %}
in which, page_filter filter must define, receives page_range list , current_page_number , return list above.
note: careful corner case, when current page near boundary. example, if 2, have return 1 [2] 3 4 5 6, instead of -2 -1 0 1 [2] 3 4 5 6. won't return page <= 0, or > maximum.
Comments
Post a Comment