Opened 2 years ago

Closed 2 years ago

#33404 closed New feature (invalid)

Make `get_elided_page_range` easier to use

Reported by: Michael Owned by:
Component: Template system Version: 4.0
Severity: Normal Keywords: pagination paginator page_obj
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: yes
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

The paginator.get_elided_page_range is an awesome function, the only problem is it's very hard to call correctly.

It requires the current page number in order to render range range correctly. Without the page number it always renders as if you are on page one.

This means when one tries to use it in the template like this:

    {% for i in paginator.get_elided_page_range %}
        ...
    {% endfor %}

It renders only correctly when one is on page one, because the paginator does not know the current page.
I stumbled across this stackoverflow question that someone else posted.

However the page_obj knows the current page number (self.number), and it has self.paginator. It's quite trivial to just delegate the call.

In file django.core.paginator.py at the very bottom just add this to class Page:

    def get_elided_page_range(self, *args, **kwargs):
        return self.paginator.get_elided_page_range(self.number, *args, **kwargs)

Then in the template one can simply do:

    {% for i in page_obj.get_elided_page_range %}
        ...
    {% endfor %}

And everything works as expected.

If the general consenseus is this is a good thing, and I am not missing something that already fixes this problem, I can try create a patch and update the documentation.

And heres a more complete example usage:

    {% for i in page_obj.get_elided_page_range %}
        {% if i == page_obj.number %}
            <div>{{ i }}</div>
        {% elif i == paginator.ELLIPSIS %}
            <div>{{ i }}</div>
        {% else %}
            <a  href="{% paginator_url i %}">{{ i }}</a>
        {% endif %}
    {% endfor %}

Change History (5)

comment:1 by Mohamed Nabil Rady, 2 years ago

Easy pickings: set
Needs documentation: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedNew feature

I think it's a great idea and it makes sense.

comment:2 by m_moeinzadeh, 2 years ago

Owner: changed from nobody to m_moeinzadeh
Status: newassigned

comment:3 by m_moeinzadeh, 2 years ago

It's impossible to do because in order for get_elided_page_range to know the page number you need to pass the number to the method directly or pass the number inside of paginator object.

comment:4 by m_moeinzadeh, 2 years ago

Owner: m_moeinzadeh removed
Status: assignednew

comment:5 by m_moeinzadeh, 2 years ago

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top