免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2299 | 回复: 0
打印 上一主题 下一主题

关于Django实现分页 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-19 17:56 |只看该作者 |倒序浏览
http://www.djangoproject.com/documentation/models/pagination/
  This example is for Django's SVN release, which can be
  significantly different from previous releases. Get old examples here:
  
   
0.96
,
  
   
0.95
.
  
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.
Model source code
from django.db import models
class Article(models.Model):
    headline = models.CharField(max_length=100, default='Default headline')
    pub_date = models.DateTimeField()
    def __unicode__(self):
        return self.headline
Sample API usage
This sample code assumes the above model has
been saved in a file mysite/models.py.
>>> from mysite.models import Article
# Prepare a list of objects for pagination.
>>> from datetime import datetime
>>> for x in range(1, 10):
...     a = Article(headline='Article %s' % x, pub_date=datetime(2005, 7, 29))
...     a.save()
##################
# Paginator/Page #
##################
>>> from django.core.paginator import Paginator
>>> 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
>>> p.object_list
[, , , , ]
>>> 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
>>> p.object_list
[, , , ]
>>> 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
# Empty pages raise EmptyPage.
>>> paginator.page(0)
Traceback (most recent call last):
...
EmptyPage: ...
>>> paginator.page(3)
Traceback (most recent call last):
...
EmptyPage: ...
# 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
>>> 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
# Paginator can be passed other objects with a count() method.
>>> class CountContainer:
...     def count(self):
...         return 42
>>> paginator = Paginator(CountContainer(), 10)
>>> paginator.count
42
>>> paginator.num_pages
5
>>> paginator.page_range
[1, 2, 3, 4, 5]
# Paginator can be passed other objects that implement __len__.
>>> class LenContainer:
...     def __len__(self):
...         return 42
>>> paginator = Paginator(LenContainer(), 10)
>>> paginator.count
42
>>> paginator.num_pages
5
>>> paginator.page_range
[1, 2, 3, 4, 5]
##################
# Orphan support #
##################
# 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()
# 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 = Paginator(Article.objects.all(), 10, orphans=1)
>>> paginator.num_pages
2
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/84280/showart_2032127.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP