|
@@ -81,22 +81,20 @@ show how you can display the results. This example assumes you have a
|
|
|
|
|
|
The view function looks like this::
|
|
|
|
|
|
- from django.core.paginator import Paginator, InvalidPage, EmptyPage
|
|
|
+ from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
|
|
|
|
|
|
def listing(request):
|
|
|
contact_list = Contacts.objects.all()
|
|
|
paginator = Paginator(contact_list, 25) # Show 25 contacts per page
|
|
|
|
|
|
- # Make sure page request is an int. If not, deliver first page.
|
|
|
- try:
|
|
|
- page = int(request.GET.get('page', '1'))
|
|
|
- except ValueError:
|
|
|
- page = 1
|
|
|
-
|
|
|
- # If page request (9999) is out of range, deliver last page of results.
|
|
|
+ page = request.GET.get('page')
|
|
|
try:
|
|
|
contacts = paginator.page(page)
|
|
|
- except (EmptyPage, InvalidPage):
|
|
|
+ except PageNotAnInteger:
|
|
|
+ # If page is not an integer, deliver first page.
|
|
|
+ contacts = paginator.page(1)
|
|
|
+ except EmptyPage:
|
|
|
+ # If page is out of range (e.g. 9999), deliver last page of results.
|
|
|
contacts = paginator.page(paginator.num_pages)
|
|
|
|
|
|
return render_to_response('list.html', {"contacts": contacts})
|
|
@@ -104,7 +102,7 @@ The view function looks like this::
|
|
|
In the template :file:`list.html`, you'll want to include navigation between
|
|
|
pages along with any interesting information from the objects themselves::
|
|
|
|
|
|
- {% for contact in contacts.object_list %}
|
|
|
+ {% for contact in contacts %}
|
|
|
{# Each "contact" is a Contact model object. #}
|
|
|
{{ contact.full_name|upper }}<br />
|
|
|
...
|
|
@@ -126,6 +124,11 @@ pages along with any interesting information from the objects themselves::
|
|
|
</span>
|
|
|
</div>
|
|
|
|
|
|
+.. versionchanged:: 1.4
|
|
|
+ Previously, you would need to use
|
|
|
+ ``{% for contact in contacts.object_list %}``, since the ``Page``
|
|
|
+ object was not iterable.
|
|
|
+
|
|
|
|
|
|
``Paginator`` objects
|
|
|
=====================
|
|
@@ -194,6 +197,7 @@ Attributes
|
|
|
|
|
|
A 1-based range of page numbers, e.g., ``[1, 2, 3, 4]``.
|
|
|
|
|
|
+
|
|
|
``InvalidPage`` exceptions
|
|
|
==========================
|
|
|
|
|
@@ -221,6 +225,9 @@ them both with a simple ``except InvalidPage``.
|
|
|
You usually won't construct :class:`Pages <Page>` by hand -- you'll get them
|
|
|
using :meth:`Paginator.page`.
|
|
|
|
|
|
+.. versionadded:: 1.4
|
|
|
+ A page acts like a sequence of :attr:`Page.object_list` when using
|
|
|
+ ``len()`` or iterating it directly.
|
|
|
|
|
|
Methods
|
|
|
-------
|