Ticket #4919: lastpage.diff

File lastpage.diff, 3.8 KB (added by nick@…, 11 years ago)

Refactored patch plus generic views documentation

  • django/views/generic/list_detail.py

     
    4747        if not page:
    4848            page = request.GET.get('page', 1)
    4949        try:
    50             page = int(page)
    51             object_list = paginator.get_page(page - 1)
    52         except (InvalidPage, ValueError):
    53             if page == 1 and allow_empty:
     50            page_number = int(page)
     51        except ValueError:
     52            if page == 'last':
     53                page_number = paginator.pages
     54            else:
     55                # Page is not 'last', nor can it be converted to an int
     56                raise Http404
     57        try:
     58            object_list = paginator.get_page(page_number - 1)
     59        except InvalidPage:
     60            if page_number == 1 and allow_empty:
    5461                object_list = []
    5562            else:
    5663                raise Http404
     
    5865            '%s_list' % template_object_name: object_list,
    5966            'is_paginated': paginator.pages > 1,
    6067            'results_per_page': paginate_by,
    61             'has_next': paginator.has_next_page(page - 1),
    62             'has_previous': paginator.has_previous_page(page - 1),
    63             'page': page,
    64             'next': page + 1,
    65             'previous': page - 1,
    66             'last_on_page': paginator.last_on_page(page - 1),
    67             'first_on_page': paginator.first_on_page(page - 1),
     68            'has_next': paginator.has_next_page(page_number - 1),
     69            'has_previous': paginator.has_previous_page(page_number - 1),
     70            'page': page_number,
     71            'next': page_number + 1,
     72            'previous': page_number - 1,
     73            'last_on_page': paginator.last_on_page(page_number - 1),
     74            'first_on_page': paginator.first_on_page(page_number - 1),
    6875            'pages': paginator.pages,
    6976            'hits' : paginator.hits,
    7077        }, context_processors)
  • docs/generic_views.txt

     
    688688    * ``paginate_by``: An integer specifying how many objects should be
    689689      displayed per page. If this is given, the view will paginate objects with
    690690      ``paginate_by`` objects per page. The view will expect either a ``page``
    691       query string parameter (via ``GET``) containing a 1-based page
    692       number, or a ``page`` variable specified in the URLconf. See
    693       "Notes on pagination" below.
     691      query string parameter (via ``GET``) or a ``page`` variable specified in
     692      the URLconf. See "Notes on pagination" below.
    694693
    695694    * ``template_name``: The full name of a template to use in rendering the
    696695      page. This lets you override the default template name (see below).
     
    777776        (r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict))
    778777
    779778    * Pass the page number via the ``page`` query-string parameter. For
    780       example, a URL would look like this:
     779      example, a URL would look like this::
    781780
    782781        /objects/?page=3
    783782
    784783In both cases, ``page`` is 1-based, not 0-based, so the first page would be
    785 represented as page ``1``.
     784represented as page ``1``.  As a special case, you are also permitted to use
     785``last`` as a value for ``page``, like so::
    786786
     787  /objects/?page=last
     788
     789This allows you to access the final page of results conveniently, without
     790first having to determine how many pages there are.
     791
     792Note that ``page`` *must* be either a valid page number or the value ``last``;
     793any other value for ``page`` will result in a 404 error.
     794
    787795``django.views.generic.list_detail.object_detail``
    788796--------------------------------------------------
    789797
Back to Top