diff --git a/django/core/paginator.py b/django/core/paginator.py
index 495cdf2..ce44a56 100644
a
|
b
|
class EmptyPage(InvalidPage):
|
10 | 10 | pass |
11 | 11 | |
12 | 12 | class 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): |
14 | 14 | self.object_list = object_list |
15 | 15 | self.per_page = per_page |
16 | 16 | self.orphans = orphans |
17 | 17 | self.allow_empty_first_page = allow_empty_first_page |
| 18 | self.request = request |
18 | 19 | self._num_pages = self._count = None |
19 | 20 | |
20 | 21 | def validate_number(self, number): |
… |
… |
class Page(object):
|
99 | 100 | def previous_page_number(self): |
100 | 101 | return self.number - 1 |
101 | 102 | |
| 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 | |
102 | 122 | def start_index(self): |
103 | 123 | """ |
104 | 124 | Returns the 1-based index of the first object on this page, |
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::
|
137 | 137 | |
138 | 138 | The :class:`Paginator` class has this constructor: |
139 | 139 | |
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) |
141 | 141 | |
142 | 142 | Required arguments |
143 | 143 | ------------------ |
… |
… |
Optional arguments
|
167 | 167 | Whether or not the first page is allowed to be empty. If ``False`` and |
168 | 168 | ``object_list`` is empty, then an ``EmptyPage`` error will be raised. |
169 | 169 | |
| 170 | ``request`` |
| 171 | An :class:`HttpRequest` object, used for creating pagination query strings. |
| 172 | |
170 | 173 | Methods |
171 | 174 | ------- |
172 | 175 | |
… |
… |
Methods
|
252 | 255 | Returns the previous page number. Note that this is "dumb" and will return |
253 | 256 | the previous page number regardless of whether a previous page exists. |
254 | 257 | |
| 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 | |
255 | 283 | .. method:: Page.start_index() |
256 | 284 | |
257 | 285 | Returns the 1-based index of the first object on the page, relative to all |