Opened 18 years ago

Closed 17 years ago

Last modified 17 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 Holovaty
Component: Generic views Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

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 18 years ago.
list_detail.diff (1.9 KB ) - added by grimboy 18 years ago.
patch.diff (4.9 KB ) - added by grimboy 18 years ago.
Oops - this is the patch in the correct diff format

Download all attachments as: .zip

Change History (9)

by grimboy, 18 years ago

Attachment: paginator.diff added

by grimboy, 18 years ago

Attachment: list_detail.diff added

comment:1 by anonymous, 18 years ago

Owner: grimboy removed

comment:2 by grimboy, 18 years ago

Owner: set to grimboy

by grimboy, 18 years ago

Attachment: patch.diff added

Oops - this is the patch in the correct diff format

comment:3 by grimboy, 18 years ago

Owner: changed from grimboy to Adrian Holovaty

comment:4 by grimboy, 18 years ago

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 by Grimboy, 17 years ago

Resolution: invalid
Status: newclosed

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 by frankie@…, 17 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top