2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2007-03-23 20:17:04 +00:00
|
|
|
30. Object pagination
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2006-05-31 19:10:43 +00:00
|
|
|
Django provides a framework for paginating a list of objects in a few lines
|
|
|
|
of code. This is often useful for dividing search results or long lists of
|
|
|
|
objects into easily readable pages.
|
2008-03-18 21:13:48 +00:00
|
|
|
|
|
|
|
In Django 0.96 and earlier, a single ObjectPaginator class implemented this
|
|
|
|
functionality. In the Django development version, the behavior is split across
|
|
|
|
two classes -- Paginator and Page -- that are more easier to use. The legacy
|
|
|
|
ObjectPaginator class is deprecated.
|
2006-05-02 01:31:56 +00:00
|
|
|
"""
|
2006-05-31 19:10:43 +00:00
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Article(models.Model):
|
2007-08-05 05:14:46 +00:00
|
|
|
headline = models.CharField(max_length=100, default='Default headline')
|
2006-05-02 01:31:56 +00:00
|
|
|
pub_date = models.DateTimeField()
|
2006-05-31 19:10:43 +00:00
|
|
|
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 12:11:04 +00:00
|
|
|
def __unicode__(self):
|
2006-05-31 19:10:43 +00:00
|
|
|
return self.headline
|
|
|
|
|
2006-08-27 13:59:47 +00:00
|
|
|
__test__ = {'API_TESTS':"""
|
2008-03-18 21:13:48 +00:00
|
|
|
# Prepare a list of objects for pagination.
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> from datetime import datetime
|
|
|
|
>>> for x in range(1, 10):
|
|
|
|
... a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
|
|
|
|
... a.save()
|
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
####################################
|
|
|
|
# New/current API (Paginator/Page) #
|
|
|
|
####################################
|
|
|
|
|
|
|
|
>>> from django.core.paginator import Paginator, InvalidPage
|
|
|
|
>>> paginator = Paginator(Article.objects.all(), 5)
|
|
|
|
>>> paginator.count
|
|
|
|
9
|
|
|
|
>>> paginator.num_pages
|
|
|
|
2
|
|
|
|
>>> paginator.page_range
|
|
|
|
[1, 2]
|
|
|
|
|
|
|
|
# Get the first page.
|
|
|
|
>>> p = paginator.page(1)
|
|
|
|
>>> p
|
|
|
|
<Page 1 of 2>
|
|
|
|
>>> p.object_list
|
|
|
|
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>, <Article: Article 5>]
|
|
|
|
>>> p.has_next()
|
|
|
|
True
|
|
|
|
>>> p.has_previous()
|
|
|
|
False
|
|
|
|
>>> p.has_other_pages()
|
|
|
|
True
|
|
|
|
>>> p.next_page_number()
|
|
|
|
2
|
|
|
|
>>> p.previous_page_number()
|
|
|
|
0
|
|
|
|
>>> p.start_index()
|
|
|
|
1
|
|
|
|
>>> p.end_index()
|
|
|
|
5
|
|
|
|
|
|
|
|
# Get the second page.
|
|
|
|
>>> p = paginator.page(2)
|
|
|
|
>>> p
|
|
|
|
<Page 2 of 2>
|
|
|
|
>>> p.object_list
|
|
|
|
[<Article: Article 6>, <Article: Article 7>, <Article: Article 8>, <Article: Article 9>]
|
|
|
|
>>> p.has_next()
|
|
|
|
False
|
|
|
|
>>> p.has_previous()
|
|
|
|
True
|
|
|
|
>>> p.has_other_pages()
|
|
|
|
True
|
|
|
|
>>> p.next_page_number()
|
|
|
|
3
|
|
|
|
>>> p.previous_page_number()
|
|
|
|
1
|
|
|
|
>>> p.start_index()
|
|
|
|
6
|
|
|
|
>>> p.end_index()
|
|
|
|
9
|
|
|
|
|
|
|
|
# Invalid pages raise InvalidPage.
|
|
|
|
>>> paginator.page(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
InvalidPage: ...
|
|
|
|
>>> paginator.page(3)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
InvalidPage: ...
|
|
|
|
|
|
|
|
# Empty paginators with allow_empty_first_page=True.
|
|
|
|
>>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=True)
|
|
|
|
>>> paginator.count
|
|
|
|
0
|
|
|
|
>>> paginator.num_pages
|
|
|
|
1
|
|
|
|
>>> paginator.page_range
|
|
|
|
[1]
|
|
|
|
|
|
|
|
# Empty paginators with allow_empty_first_page=False.
|
|
|
|
>>> paginator = Paginator(Article.objects.filter(id=0), 5, allow_empty_first_page=False)
|
|
|
|
>>> paginator.count
|
|
|
|
0
|
|
|
|
>>> paginator.num_pages
|
|
|
|
0
|
|
|
|
>>> paginator.page_range
|
|
|
|
[]
|
|
|
|
|
|
|
|
# Paginators work with regular lists/tuples, too -- not just with QuerySets.
|
|
|
|
>>> paginator = Paginator([1, 2, 3, 4, 5, 6, 7, 8, 9], 5)
|
|
|
|
>>> paginator.count
|
|
|
|
9
|
|
|
|
>>> paginator.num_pages
|
|
|
|
2
|
|
|
|
>>> paginator.page_range
|
|
|
|
[1, 2]
|
|
|
|
|
|
|
|
# Get the first page.
|
|
|
|
>>> p = paginator.page(1)
|
|
|
|
>>> p
|
|
|
|
<Page 1 of 2>
|
|
|
|
>>> p.object_list
|
|
|
|
[1, 2, 3, 4, 5]
|
|
|
|
>>> p.has_next()
|
|
|
|
True
|
|
|
|
>>> p.has_previous()
|
|
|
|
False
|
|
|
|
>>> p.has_other_pages()
|
|
|
|
True
|
|
|
|
>>> p.next_page_number()
|
|
|
|
2
|
|
|
|
>>> p.previous_page_number()
|
|
|
|
0
|
|
|
|
>>> p.start_index()
|
|
|
|
1
|
|
|
|
>>> p.end_index()
|
|
|
|
5
|
|
|
|
|
|
|
|
################################
|
|
|
|
# Legacy API (ObjectPaginator) #
|
|
|
|
################################
|
|
|
|
|
2008-03-18 21:53:39 +00:00
|
|
|
# Don't print out the deprecation warnings during testing.
|
|
|
|
>>> from warnings import filterwarnings
|
|
|
|
>>> filterwarnings("ignore")
|
|
|
|
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> from django.core.paginator import ObjectPaginator, InvalidPage
|
|
|
|
>>> paginator = ObjectPaginator(Article.objects.all(), 5)
|
|
|
|
>>> paginator.hits
|
|
|
|
9
|
|
|
|
>>> paginator.pages
|
|
|
|
2
|
2008-03-18 21:13:48 +00:00
|
|
|
>>> paginator.page_range
|
|
|
|
[1, 2]
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
# Get the first page.
|
2006-05-31 19:10:43 +00:00
|
|
|
>>> paginator.get_page(0)
|
2006-06-04 00:23:51 +00:00
|
|
|
[<Article: Article 1>, <Article: Article 2>, <Article: Article 3>, <Article: Article 4>, <Article: Article 5>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> paginator.has_next_page(0)
|
|
|
|
True
|
|
|
|
>>> paginator.has_previous_page(0)
|
|
|
|
False
|
2008-03-18 21:13:48 +00:00
|
|
|
>>> paginator.first_on_page(0)
|
|
|
|
1
|
|
|
|
>>> paginator.last_on_page(0)
|
|
|
|
5
|
2006-05-02 01:31:56 +00:00
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
# Get the second page.
|
|
|
|
>>> paginator.get_page(1)
|
|
|
|
[<Article: Article 6>, <Article: Article 7>, <Article: Article 8>, <Article: Article 9>]
|
2006-05-02 01:31:56 +00:00
|
|
|
>>> paginator.has_next_page(1)
|
|
|
|
False
|
|
|
|
>>> paginator.has_previous_page(1)
|
|
|
|
True
|
2006-06-01 04:29:39 +00:00
|
|
|
>>> paginator.first_on_page(1)
|
|
|
|
6
|
|
|
|
>>> paginator.last_on_page(1)
|
|
|
|
9
|
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
# Invalid pages raise InvalidPage.
|
|
|
|
>>> paginator.get_page(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
InvalidPage: ...
|
|
|
|
>>> paginator.get_page(2)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
InvalidPage: ...
|
|
|
|
|
|
|
|
# Empty paginators with allow_empty_first_page=True.
|
|
|
|
>>> paginator = ObjectPaginator(Article.objects.filter(id=0), 5)
|
|
|
|
>>> paginator.count
|
|
|
|
0
|
|
|
|
>>> paginator.num_pages
|
|
|
|
1
|
|
|
|
>>> paginator.page_range
|
|
|
|
[1]
|
|
|
|
|
|
|
|
##################
|
|
|
|
# Orphan support #
|
|
|
|
##################
|
|
|
|
|
2006-11-07 04:44:27 +00:00
|
|
|
# Add a few more records to test out the orphans feature.
|
|
|
|
>>> for x in range(10, 13):
|
|
|
|
... Article(headline="Article %s" % x, pub_date=datetime(2006, 10, 6)).save()
|
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
# With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
|
|
|
|
>>> paginator = Paginator(Article.objects.all(), 10, orphans=3)
|
|
|
|
>>> paginator.num_pages
|
|
|
|
1
|
|
|
|
|
|
|
|
# With orphans only set to 1, we should get two pages.
|
|
|
|
>>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=1)
|
|
|
|
>>> paginator.num_pages
|
|
|
|
2
|
|
|
|
|
|
|
|
# LEGACY: With orphans set to 3 and 10 items per page, we should get all 12 items on a single page.
|
2006-11-07 04:44:27 +00:00
|
|
|
>>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=3)
|
|
|
|
>>> paginator.pages
|
|
|
|
1
|
|
|
|
|
2008-03-18 21:13:48 +00:00
|
|
|
# LEGACY: With orphans only set to 1, we should get two pages.
|
2006-11-07 04:44:27 +00:00
|
|
|
>>> paginator = ObjectPaginator(Article.objects.all(), 10, orphans=1)
|
|
|
|
>>> paginator.pages
|
|
|
|
2
|
2006-08-27 13:59:47 +00:00
|
|
|
"""}
|