Django

Code

Changeset 8129

Show
Ignore:
Timestamp:
07/28/08 00:30:35 (4 months ago)
Author:
gwilson
Message:

Fixed #6997 -- Corrected num_pages calculation when one item is in the object list and allow_empty_first_page=False, thanks to framos for the report. Also, made Page's start_index() return 0 if there are no items in the object list (previously it was returning 1, but now it is consistent with end_index()). As an added bonus, I threw in quite a few pagination tests.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/paginator.py

    r8121 r8129  
     1from math import ceil 
     2 
    13class InvalidPage(Exception): 
    24    pass 
     
    5658        "Returns the total number of pages." 
    5759        if self._num_pages is None: 
    58             hits = self.count - 1 - self.orphans 
    59             if hits < 1: 
    60                 hits = 0 
    61             if hits == 0 and not self.allow_empty_first_page: 
     60            if self.count == 0 and not self.allow_empty_first_page: 
    6261                self._num_pages = 0 
    6362            else: 
    64                 self._num_pages = hits // self.per_page + 1 
     63                hits = max(1, self.count - self.orphans) 
     64                self._num_pages = int(ceil(hits / float(self.per_page))) 
    6565        return self._num_pages 
    6666    num_pages = property(_get_num_pages) 
     
    105105        relative to total objects in the paginator. 
    106106        """ 
     107        # Special case, return zero if no items. 
     108        if self.paginator.count == 0: 
     109            return 0 
    107110        return (self.paginator.per_page * (self.number - 1)) + 1 
    108111 
     
    112115        relative to total objects found (hits). 
    113116        """ 
     117        # Special case for the last page because there can be orphans. 
    114118        if self.number == self.paginator.num_pages: 
    115119            return self.paginator.count