#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/053.cc9e2fcb8fa70cb4482a62880e2afdd9%40djangoproject.com.

Reply via email to