How to use get_elided_page_range in Django paginator?

Issue

There is new option to create pagination range – get_elided_page_range
https://docs.djangoproject.com/en/3.2/ref/paginator/#django.core.paginator.Paginator.get_elided_page_range

How should I use it? How can I set args? I am using CBV ListView.
I tried https://nemecek.be/blog/105/how-to-use-elided-pagination-in-django-and-solve-too-many-pages-problem but it didn’t work for me.

I have 81 pages and current page is 10. Problem is I am always have range 1 2 3 4 … 80 81
What am I doing wrong?

#views.py

class TrailersListView(ListView):
    queryset = Trailer.objects.all()
    paginate_by = 10

#template.html

{% for i in paginator.get_elided_page_range %}
    {% if page_obj.number == i %}
        <li class="active page-item">
            <span class="page-link">{{ i }}</span>
        </li>
    {% else %}
        {% if i == paginator.ELLIPSIS %}
            <li class="page-item">
                <span class="page-link">{{ paginator.ELLIPSIS }}</span>
            </li>
        {% else %}
            <li class="page-item">
                <a class="page-link" href="?page={{ i }}">{{ i }}</a>
            </li>
        {% endif %}
    {% endif %}
{% endfor %}

Solution

The page number is not passed to the "get_elided_page_range" method and it uses the default value (1) all the time. So I created a custom tag to pass it a number.

appname/templatetags/paginator_tags.py

from django import template
from django.core.paginator import Paginator

register = template.Library()


@register.simple_tag
def get_proper_elided_page_range(p, number, on_each_side=3, on_ends=2):
    paginator = Paginator(p.object_list, p.per_page)
    return paginator.get_elided_page_range(number=number, 
                                           on_each_side=on_each_side,
                                           on_ends=on_ends)

template.html

{% load paginator_tags %}

{% get_proper_elided_page_range paginator page_obj.number as page_range %} <!-- here -->
{% for page_num in page_range %}
    {% if page_obj.number == i %}
        <li class="active page-item">
            <span class="page-link">{{ i }}</span>
        </li>
    {% else %}
        {% if i == paginator.ELLIPSIS %}
            <li class="page-item">
                <span class="page-link">{{ paginator.ELLIPSIS }}</span>
            </li>
        {% else %}
            <li class="page-item">
                <a class="page-link" href="?page={{ i }}">{{ i }}</a>
            </li>
        {% endif %}
    {% endif %}
{% endfor %}

working paginator

You can add additional arguments, e.g:

{% get_proper_elided_page_range paginator page_obj.number 1 1 as page_range %}

paginator example with arguments

Answered By – v0kan

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published