[Python] Django中使用Pagination的分页范例代码 →→→→→进入此内容的聊天室

来自 , 2020-11-04, 写在 Python, 查看 112 次.
URL http://www.code666.cn/view/63c3ddcc
  1. >>> from django.core.paginator import Paginator
  2. >>> objects = ['john', 'paul', 'george', 'ringo']
  3. >>> p = Paginator(objects, 2)
  4.  
  5. >>> p.count
  6. 4
  7. >>> p.num_pages
  8. 2
  9. >>> p.page_range
  10. [1, 2]
  11.  
  12. >>> page1 = p.page(1)
  13. >>> page1
  14. <Page 1 of 2>
  15. >>> page1.object_list
  16. ['john', 'paul']
  17.  
  18. >>> page2 = p.page(2)
  19. >>> page2.object_list
  20. ['george', 'ringo']
  21. >>> page2.has_next()
  22. False
  23. >>> page2.has_previous()
  24. True
  25. >>> page2.has_other_pages()
  26. True
  27. >>> page2.next_page_number()
  28. Traceback (most recent call last):
  29. ...
  30. EmptyPage: That page contains no results
  31. >>> page2.previous_page_number()
  32. 1
  33. >>> page2.start_index() # The 1-based index of the first item on this page
  34. 3
  35. >>> page2.end_index() # The 1-based index of the last item on this page
  36. 4
  37.  
  38. >>> p.page(0)
  39. Traceback (most recent call last):
  40. ...
  41. EmptyPage: That page number is less than 1
  42. >>> p.page(3)
  43. Traceback (most recent call last):
  44. ...
  45. EmptyPage: That page contains no results
  46. #//python/8929

回复 "Django中使用Pagination的分页范例代码"

这儿你可以回复上面这条便签

captcha