Django paginator page range for not displaying all numbers

I.Jokhadze picture I.Jokhadze · Dec 13, 2016 · Viewed 10k times · Source

I have a pagination in my site but it shows me every page like 1-19, i only want to display only 5 pages.enter image description here

How can i do this?

views.py

    paginator = Paginator(object_list, new_number_of_list)

    page = request.GET.get('page')

    try:
        Items = paginator.page(page)
    except PageNotAnInteger:
        Items = paginator.page(1)
    except EmptyPage:
        Items = paginator.page(paginator.num_pages)

    variables = RequestContext(request, {"Items": Items,
                                         "ForItemCount": ForItemCount,
                                         "page": page,
                                         })
    return render(request, 'templates/Core/resource_base.html',
                           variables)

my pagination.html

<div class="pagination p1">
  <span class="step-links">
    <ul>

          {% for l in  Items.paginator.page_range %}
            <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
          {% endfor %}



    </ul>
  </span>
</div>

Answer

Andrysha picture Andrysha · Mar 26, 2018

Another quick solution that can be used(+ and - 5 limit for example):

{% for l in  Items.paginator.page_range %}
    {% if l <= Items.number|add:5 and l >= Items.number|add:-5 %}
        <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
    {% endif %}
{% endfor %}