Ticket #10941: querystring-methods.diff

File querystring-methods.diff, 3.8 KB (added by Ben Spaulding, 15 years ago)

Methods for handling pagination query strings, documentation.

  • django/core/paginator.py

    diff --git a/django/core/paginator.py b/django/core/paginator.py
    index 495cdf2..ce44a56 100644
    a b class EmptyPage(InvalidPage):  
    1010    pass
    1111
    1212class Paginator(object):
    13     def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True):
     13    def __init__(self, object_list, per_page, orphans=0, allow_empty_first_page=True, request=None):
    1414        self.object_list = object_list
    1515        self.per_page = per_page
    1616        self.orphans = orphans
    1717        self.allow_empty_first_page = allow_empty_first_page
     18        self.request = request
    1819        self._num_pages = self._count = None
    1920
    2021    def validate_number(self, number):
    class Page(object):  
    99100    def previous_page_number(self):
    100101        return self.number - 1
    101102
     103    def _other_page_querystring(self, page_number):
     104        """
     105        Returns a query string for the given page, preserving any
     106        GET parameters present.
     107        """
     108        try:
     109            querydict = self.paginator.request.GET.copy()
     110            querydict['page'] = page_number
     111            querystring = querydict.urlencode()
     112        except AttributeError:
     113            querystring = 'page=%s' % page_number
     114        return querystring
     115
     116    def next_page_querystring(self):
     117        return self._other_page_querystring(self.next_page_number())
     118
     119    def previous_page_querystring(self):
     120        return self._other_page_querystring(self.previous_page_number())
     121
    102122    def start_index(self):
    103123        """
    104124        Returns the 1-based index of the first object on this page,
  • docs/topics/pagination.txt

    diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt
    index 4cb2fec..38e98cb 100644
    a b pages along with any interesting information from the objects themselves::  
    137137
    138138The :class:`Paginator` class has this constructor:
    139139
    140 .. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
     140.. class:: Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True, request=None)
    141141
    142142Required arguments
    143143------------------
    Optional arguments  
    167167    Whether or not the first page is allowed to be empty.  If ``False`` and
    168168    ``object_list`` is  empty, then an ``EmptyPage`` error will be raised.
    169169
     170``request``
     171    An :class:`HttpRequest` object, used for creating pagination query strings.
     172
    170173Methods
    171174-------
    172175
    Methods  
    252255    Returns the previous page number. Note that this is "dumb" and will return
    253256    the previous page number regardless of whether a previous page exists.
    254257
     258.. method:: Page.next_page_querystring()
     259
     260    Returns the query string for the next page. This is useful when pagination
     261    is handled with a query string (as opposed to a URL pattern). It is common
     262    for there to be other parameters in the query string that need to remain
     263    intact on the the next page e.g, paginated search results.
     264
     265    Note that this is "dumb" and will return the next query string regardless
     266    of whether a subsequent page exists.
     267
     268    .. admonition:: Where's my query string?
     269
     270        If the :class:`Page`'s associated :class:`Paginator` was not passed an
     271        :class:`HttpRequest` object when it was instantiated, this method will
     272        only return the ``page`` parameter. In order to return the whole
     273        query string the :class:`Paginator` must have been constructed with a
     274        valid ``request`` argument.
     275
     276.. method:: Page.previous_page_querystring()
     277
     278    Returns the query string for the previous page. See :meth:`~Page.next_page_querystring`.
     279
     280    Note that this is "dumb" and will return the previous query string
     281    regardless of whether a previous page exists.
     282
    255283.. method:: Page.start_index()
    256284
    257285    Returns the 1-based index of the first object on the page, relative to all
Back to Top