Code

Opened 8 years ago

Closed 7 years ago

Last modified 7 years ago

#2574 closed enhancement (invalid)

[patch] object_list passes in list of page numbers, the first and last object numbers and accepts lists as well as querysets

Reported by: grimboy Owned by: adrian
Component: Generic views Version: master
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

This patch adds three improvements.

The first improvement this patch adds is that the object_list view passes in a list of page numbers called page_list to the template. By default this list contains all the valid pages. The arguements page_list_surround and page_list_edges are two integers that can be passed in to override the default (in case your results have too many pages). page_list_surround defines how many pages on each side of the current page should be included in page_list, so if the current page is 5 and there are 10 pages a page_list_surround of 3 would pass the template a list like ["sep", 2, 3, 4, 5, 6, 7, 8, "sep"]. page_list_edges allows you to include the first and last few pages as well, so if the current page is 10, there are 20 pages and page_list_surround is 3 a page_list_edges of 3 would pass the template a list like [1, 2, 3, "sep", 7, 8, 9, 10, 11, 12, 13, "sep", 18, 19, 20]. The sep strings are there to show that more pages exist. This should be overridden using simple template logic such as {% ifequal other_page "sep" %}...{% endifqual %} or it can be ignored by putting everything within the loop of page_list in a {% ifnotequal other_page "sep" %} tag.

That may have been slightly badly explained so here is a template example:

{% if is_paginated %}
    <div class="Paginator">
        {% for other_page in page_list %}
            {% ifequal other_page page %}
                <span class="this-page">{{ page }}</span>
            {% else %}{% ifequal other_page "sep" %}
                ...
            {% else %}
                <a
                {% ifequal other_page previous %}
                    class="previous"
                {% else %}{% ifequal other_page next %}
                    class="next"
                {% endifequal %}{% endifequal %}
                href="../{{ other_page }}/{% if querystring %}?{{ querystring|escape }}{% endif %}">{{ other_page }}</a>
            {% endifequal %}{% endifequal %}
        {% endfor %}
    </div>
    <br style="clear: right;" />
{% endif %}

With a little css the above looks like this:
http://img292.imageshack.us/img292/9097/pagerp2.png

The second improvement this patch makes is that object_list now passes the already existing paginator attributes first_on_page and last_on_page so that the resulting page can say something like "1 - 10 of 22".

The third and final improvement is that as well as accepting querysets for the arguement queryset, queryset can now be a list, which is important if a view requires pagination but only has data not from a database, data from a database that has been processed or data from a database obtained via raw SQL.

I'm not sure if some of these improvements go against the django way, if they do then implement any of it that is django-y and leave out the rest.

Attachments (3)

paginator.diff (1.4 KB) - added by grimboy 8 years ago.
list_detail.diff (1.9 KB) - added by grimboy 8 years ago.
patch.diff (4.9 KB) - added by grimboy 8 years ago.
Oops - this is the patch in the correct diff format

Download all attachments as: .zip

Change History (9)

Changed 8 years ago by grimboy

Changed 8 years ago by grimboy

comment:1 Changed 8 years ago by anonymous

  • Owner grimboy deleted

comment:2 Changed 8 years ago by grimboy

  • Owner set to grimboy

Changed 8 years ago by grimboy

Oops - this is the patch in the correct diff format

comment:3 Changed 8 years ago by grimboy

  • Owner changed from grimboy to adrian

comment:4 Changed 8 years ago by grimboy

The part of the third improvement to do with the paginator has been done in this ticket http://code.djangoproject.com/ticket/2575 in a more elegant way.

comment:5 Changed 7 years ago by Grimboy

  • Resolution set to invalid
  • Status changed from new to closed

Alright, I've looked this over and it's not very neat. I'll do the same few things in a number of smaller patches. That way it is easier to pick and mix stuff. Oh and I'll change documentation this time.

comment:6 Changed 7 years ago by frankie@…

Oh, and for the sake of completeness here they are:

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.