9

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>
I.Jokhadze
  • 456
  • 2
  • 8
  • 27

3 Answers3

25

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 %}
Andrysha
  • 371
  • 3
  • 9
7

You already have the {{ forloop.counter }} in there, you can use that to limit it to five.

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

Here is another solution, which might give you some more options if you're interested: Django Pagination Display Issue: all the page numbers show up.

And finally, here is a very nice tutorial on getting up to speed with the pagination that django offers out of the box: https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html.

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
1

See if https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432 helps. The code uses 10 for "neighbors" but you could change it to 5 when using the tag.

LNI
  • 2,935
  • 2
  • 21
  • 25
  • It's a good solution btw, but your `link_name` works wrong. This version works correct: `@register.filter(name='link_name') def link_name(path, page_number): url = urlparse(path) qs = parse_qs(url[4]) page_number = str(page_number) qs['page'] = page_number return urlunparse((url[0], url[1], url[2], url[3], urlencode(qs), url[5])) ` – el fuego Feb 10 '18 at 23:16
  • Thanks @elfuego, can you tell me what was failing that required you to add the additional code, and what Django version were you using? I deployed this on a production site and didn't run into any issues, but was only testing on 1.11. – LNI Feb 12 '18 at 23:38
  • 1
    The actual problem is every time you click on a page link, an additional `&page=X` is added to the link, so eventually you get `...?page=X&page=Y...`. My Django version is 2.0.1, my Python version is `3.6.2`. – el fuego Feb 14 '18 at 07:28
  • I mean, if you have at least 2 pages, and if you click to page 2, it adds `?page=2` to the link, and if you then click to page 1, it adds _another_ `&page=1` and it becomes `?page=2&page=1`, while it should rewrite the previous page. – el fuego Feb 14 '18 at 07:50
  • 1
    Thanks @elfuego, that's a great catch! Will fix in my code and republish. – LNI Feb 14 '18 at 19:20