- 论坛徽章:
- 0
|
举例¶
类Paginator,带两个构造参数,一个就是数据的集合,另一个表示每页放几个数据。
>>> from django.core.paginator import Paginator
>>> objects = ['john', 'paul', 'george', 'ringo']
>>> p = Paginator(objects, 2)
>>> p.count
4
>>> p.num_pages
2
>>> p.page_range
[1, 2]
>>> page1 = p.page(1)
>>> page1
<Page 1 of 2>
>>> page1.object_list
['john', 'paul']
>>> page2 = p.page(2)
>>> page2.object_list
['george', 'ringo']
>>> page2.has_next()
False
>>> page2.has_previous()
True
>>> page2.has_other_pages()
True
>>> page2.next_page_number()
3
>>> page2.previous_page_number()
1
>>> page2.start_index() # The 1-based index of the first item on this page
3
>>> page2.end_index() # The 1-based index of the last item on this page
4
>>> p.page(0)
...
EmptyPage: That page number is less than 1
>>> p.page(3)
...
EmptyPage: That page contains no results
Note
Paginator 的第一个参数可以是list,tuple,QuerySet 或者任意对象————只要它有 count() 或者 __len__() 函数。 Django后台会先尝试调用 count()``。如果 不可行,再定要 ``len()
欢迎加入88519564 QQ 群一起交流共同进步。 |
|