Django

Code

Changeset 6149

Show
Ignore:
Timestamp:
09/13/07 20:52:10 (1 year ago)
Author:
russellm
Message:

Fixed #4919 -- Added 'last' marker on paginators. Thanks to patrick@vonautomatisch.atfor the idea, and nick@efford.org for the patch and docs.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/generic/list_detail.py

    r6146 r6149  
    5050            page = request.GET.get('page', 1) 
    5151        try: 
    52             page = int(page) 
    53             object_list = paginator.get_page(page - 1) 
    54         except (InvalidPage, ValueError): 
    55             if page == 1 and allow_empty: 
     52            page_number = int(page) 
     53        except ValueError: 
     54            if page == 'last': 
     55                page_number = paginator.pages 
     56            else: 
     57                # Page is not 'last', nor can it be converted to an int 
     58                raise Http404 
     59        try: 
     60            object_list = paginator.get_page(page_number - 1) 
     61        except InvalidPage: 
     62            if page_number == 1 and allow_empty: 
    5663                object_list = [] 
    5764            else: 
     
    6168            'is_paginated': paginator.pages > 1, 
    6269            'results_per_page': paginate_by, 
    63             'has_next': paginator.has_next_page(page - 1), 
    64             'has_previous': paginator.has_previous_page(page - 1), 
    65             'page': page
    66             'next': page + 1, 
    67             'previous': page - 1, 
    68             'last_on_page': paginator.last_on_page(page - 1), 
    69             'first_on_page': paginator.first_on_page(page - 1), 
     70            'has_next': paginator.has_next_page(page_number - 1), 
     71            'has_previous': paginator.has_previous_page(page_number - 1), 
     72            'page': page_number
     73            'next': page_number + 1, 
     74            'previous': page_number - 1, 
     75            'last_on_page': paginator.last_on_page(page_number - 1), 
     76            'first_on_page': paginator.first_on_page(page_number - 1), 
    7077            'pages': paginator.pages, 
    7178            'hits' : paginator.hits, 
  • django/trunk/docs/generic_views.txt

    r6146 r6149  
    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 
     
    781780 
    782781    * Pass the page number via the ``page`` query-string parameter. For 
    783       example, a URL would look like this: 
     782      example, a URL would look like this:: 
    784783 
    785784        /objects/?page=3 
     
    790789 
    791790These values and lists are is 1-based, not 0-based, so the first page would be 
    792 represented as page ``1``. 
     791represented as page ``1``. As a special case, you are also permitted to use  
     792``last`` as a value for ``page``:: 
     793 
     794    /objects/?page=last 
     795 
     796This allows you to access the final page of results without first having to  
     797determine how many pages there are. 
     798 
     799Note that ``page`` *must* be either a valid page number or the value ``last``; 
     800any other value for ``page`` will result in a 404 error. 
    793801 
    794802``django.views.generic.list_detail.object_detail``