diff options
| author | Chris Beaven <smileychris@gmail.com> | 2011-04-06 09:40:26 +0000 |
|---|---|---|
| committer | Chris Beaven <smileychris@gmail.com> | 2011-04-06 09:40:26 +0000 |
| commit | 4fa96467164d738c6f8a9c59423382257f95b747 (patch) | |
| tree | efe28ce70d1391236201fa0c289e5ee2a2a8dca7 /docs | |
| parent | de5f07513bb3fb8be4131685c6d3892219d8d821 (diff) | |
Fixes #11596 -- Make paginator.Page iterable
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16018 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/topics/pagination.txt | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/docs/topics/pagination.txt b/docs/topics/pagination.txt index db776aaf00..2ae3d5415e 100644 --- a/docs/topics/pagination.txt +++ b/docs/topics/pagination.txt @@ -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 ------- |
