Django之分页

mac2022-06-30  96

分页器paginator

准备工作

分页器使用前,先导入,顺便导入页面为空和页码不是整数异常

from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

简单示范

以之前book表为例

book_list=Book.objects.all() paginator = Paginator(book_list, 10) print("count:",paginator.count) #数据总条数 print("num_pages",paginator.num_pages) #总页数 print("page_range",paginator.page_range) #页码的列表 page1=paginator.page(1) #第1页的page对象 for i in page1: #遍历第1页的所有数据对象 print(i) print(page1.object_list) #第1页的所有数据 page2=paginator.page(2) print(page2.has_next()) #是否有下一页 print(page2.next_page_number()) #下一页的页码 print(page2.has_previous()) #是否有上一页 print(page2.previous_page_number()) #上一页的页码 # 抛错 #page=paginator.page(12) # error:EmptyPage #page=paginator.page("z") # error:PageNotAnInteger

完善版

模板

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <link rel="stylesheet" href="\static\bootstrap-3.3.7-dist\css\bootstrap.css"> 7 </head> 8 <body> 9 <div class="container"> 10 <div class="row"> 11 <div class="col-md-6 col-md-offset-3"> 12 <table class="table table-striped table-bordered"> 13 <thead> 14 <th>书名</th> 15 <th>价格</th> 16 </thead> 17 <tbody> 18 {% for book in current_page %} 19 <tr> 20 <td>{{ book.title }}</td> 21 <td>{{ book.price }}</td> 22 </tr> 23 {% endfor %} 24 </tbody> 25 </table> 26 <nav aria-label="Page navigation"> 27 <ul class="pagination"> 28 {% if current_page.has_previous %} 29 <li> 30 <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous"> 31 <span aria-hidden="true">«</span> 32 </a> 33 </li> 34 {% else %} 35 <li class="disabled"> 36 <a href="#" aria-label="Previous"> 37 <span aria-hidden="true">«</span> 38 </a> 39 </li> 40 {% endif %} 41 42 {% for page in page_range %} 43 {% if page == page_num %} {# current_page 是页面数据对象,不能在这里和page比较#} 44 <li class="active"><a href="?page={{ page }}">{{ page }}</a></li> 45 {% else %} 46 <li><a href="?page={{ page }}">{{ page }}</a></li> 47 {% endif %} 48 49 {% endfor %} 50 51 {% if current_page.has_next %} 52 <li> 53 <a href="?page={{ current_page.next_page_number }}" aria-label="Next"> 54 <span aria-hidden="true">»</span> 55 </a> 56 </li> 57 {% else %} 58 <li class="disabled"> 59 <a href="#" aria-label="Next"> 60 <span aria-hidden="true">»</span> 61 </a> 62 </li> 63 {% endif %} 64 65 </ul> 66 </nav> 67 </div> 68 </div> 69 </div> 70 </body> 71 </html> book.html

视图

1 def book(request): 2 book_list = Book.objects.all() 3 paginator = Paginator(book_list, 5) # 对book_list进行分页,每页5项 4 page_num = int(request.GET.get('page', 1)) # 获取页面,第一次没有获取请求,设置为1 5 6 # print("count:", paginator.count) # 数据总数 7 # print("num_pages", paginator.num_pages) # 总页数 8 # print("page_range", paginator.page_range) # 页码的列表 9 10 if paginator.num_pages < 11: 11 page_range = paginator.page_range 12 else: 13 if page_num < 6: 14 page_range = range(1, 12) 15 elif page_num + 5 > paginator.num_pages: 16 page_range = range(paginator.num_pages - 10, paginator.num_pages + 1) 17 else: 18 page_range = range(page_num - 5, page_num + 5) 19 try: 20 current_page = paginator.page(page_num) # 获取某一页数据对象 21 except EmptyPage: 22 current_page = paginator.page(1) # 如果获取不到,使其获取第一页数据 23 return render(request, 'book.html', locals())

效果图

 

转载于:https://www.cnblogs.com/yuliangkaiyue/p/9939459.html

最新回复(0)