[Django] #33404: Make `get_elided_page_range` easier to use

188 views
Skip to first unread message

Django

unread,
Jan 1, 2022, 3:20:50 PM1/1/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: | Status: new
Uncategorized |
Component: Template | Version: 4.0
system | Keywords: pagination
Severity: Normal | paginator page_obj
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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 [https://stackoverflow.com/questions/69277936/how-
to-use-get-elided-page-range-in-django-paginator 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 %}
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33404>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jan 1, 2022, 4:54:53 PM1/1/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: nobody
Type: New feature | Status: new
Component: Template system | Version: 4.0
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
paginator page_obj |
Has patch: 0 | Needs documentation: 1

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mohamed Nabil Rady):

* needs_docs: 0 => 1
* type: Uncategorized => New feature
* easy: 0 => 1
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/33404#comment:1>

Django

unread,
Jan 3, 2022, 7:21:24 AM1/3/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner:
| m_moeinzadeh
Type: New feature | Status: assigned

Component: Template system | Version: 4.0
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
paginator page_obj |
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by m_moeinzadeh):

* owner: nobody => m_moeinzadeh
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/33404#comment:2>

Django

unread,
Jan 5, 2022, 4:44:34 AM1/5/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner:
| m_moeinzadeh
Type: New feature | Status: assigned
Component: Template system | Version: 4.0
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
paginator page_obj |
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by m_moeinzadeh):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/33404#comment:3>

Django

unread,
Jan 5, 2022, 4:45:14 AM1/5/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: (none)

Type: New feature | Status: new
Component: Template system | Version: 4.0
Severity: Normal | Resolution:
Keywords: pagination | Triage Stage: Accepted
paginator page_obj |
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by m_moeinzadeh):

* owner: m_moeinzadeh => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/33404#comment:4>

Django

unread,
Jan 5, 2022, 4:45:29 AM1/5/22
to django-...@googlegroups.com
#33404: Make `get_elided_page_range` easier to use
-------------------------------------+-------------------------------------
Reporter: Michael | Owner: (none)
Type: New feature | Status: closed

Component: Template system | Version: 4.0
Severity: Normal | Resolution: invalid

Keywords: pagination | Triage Stage: Accepted
paginator page_obj |
Has patch: 0 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by m_moeinzadeh):

* status: new => closed
* resolution: => invalid


--
Ticket URL: <https://code.djangoproject.com/ticket/33404#comment:5>

Reply all
Reply to author
Forward
0 new messages