11.Django的分页器paginator
Django的分页器paginator
01.views:
1 from django.shortcuts import render, HttpResponse, redirect
2 from app01.models import Book
3 from django.core.paginator import Paginator, EmptyPage # 引入分页器
4
5
6 def index(request):
7 '''
8 批量插入数据
9 book_list = []
10 for i in range(100): # 插入数据
11 book = Book(name="book_%s" % i,price=i*i)
12 book_list.append(book) # 添加book对象到列表里
13
14 Book.objects.bulk_create(book_list) # 批量保存数据
15
16 :param request:
17 :return:
18 '''
19
20 book_list = Book.objects.all()
21
22 # 分页器
23
24 paginator = Paginator(book_list, 8) # 参数1:数据/参数2:每页数据条数
25
26 print("count:", paginator.count) # 数据总数
27 print("num_pages:", paginator.num_pages) # 总页数
28 print("page_range:", paginator.page_range) # 页码的列表
29
30 current_page_num = int(request.GET.get("page", 1)) # 动态的拿到第几页的数据,默认拿到第一页
31 print("Num:", current_page_num)
32
33 # 设置分页样式,前6也,后5也无变化/中间的样式:选择的页数始终位于分页器中央
34 if paginator.num_pages > 9: # 当总页数大于9
35
36 if current_page_num - 5 < 1: # 设置前6页不变
37 page_range = range(1, 11) # 设置显示的页码列表11个
38 elif current_page_num + 5 > paginator.num_pages:
39 page_range = range(paginator.num_pages - 11, paginator.num_pages + 1)
40 else:
41 page_range = range(current_page_num - 5, current_page_num + 6)
42 else:
43 page_range = paginator.page_range
44
45 try: # 如果分页器超出页数或小于页数,捕获异常
46
47
48 current_page = paginator.page(current_page_num) # 拿到第几页的配置对象
49
50 # page1 = paginator.page(1) # 拿到第1页的数据的对象
51 #
52 # #显示某一页具体数据的两种方式
53 # print("object_list",page1.object_list) # 拿到第1页的数据
54
55 for i in current_page: # 迭代拿第一页的每条数据对象
56 print(i)
57
58 except EmptyPage as e:
59 current_page = paginator.page(1)
60
61 return render(request, 'index.html', locals())
02.index.html:
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
4 <meta charset="UTF-8">
5 <title>index</title>
6 <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"
7 integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
8 </head>
9 <body>
10 {#每页数据内容#}
11 <ul>
12 {% for book in current_page %}
13 <li>{{ book.name }}:{{ book.price }}</li>
14 {% endfor %}
15 </ul>
16
17 {#分页器#}
18 <nav aria-label="Page navigation">
19 <ul class="pagination">
20 {% if current_page.has_previous %}{# 如果分页器配置对象是否还有上一页 #}
21 <li>
22 <a href="?page={{ current_page.previous_page_number }}" aria-label="Previous">{# 如果有上一页,点击上一页则会跳转上一页的page地址 #}
23 <span aria-hidden="true">«</span>
24 </a>
25 </li>
26 {% else %}{# 如果没有上一页,就对上一页按钮进行设置class="disabled",隐藏按钮 #}
27 <li class="disabled">
28 <a href="" aria-label="Previous">{# 取消跳转地址 #}
29 <span aria-hidden="true">«</span>
30 </a>
31 </li>
32 {% endif %}
33
34 {% for item in page_range %}{# 循环页码列表page_range,拿到每页页码 #}
35 {% if current_page_num == item %}{# 拿到当前页数 #}
36 <li class="active"><a href="?page={{ item }}">{{ item }}</a></li>{# bootstrap样式avtive为选择器,a标签跳转到选择的页数 #}
37 {% else %}
38 <li><a href="?page={{ item }}">{{ item }}</a></li>{# 没有选中的不给active #}
39 {% endif %}
40 {% endfor %}
41 {% if current_page.has_next %}{# 判断是否有下一个值 #}
42 <li>
43 <a href="?page={{ current_page.next_page_number }}" aria-label="Next">
44 <span aria-hidden="true">»</span>
45 </a>
46 </li>
47 {% else %}
48 <li class="disabled">
49 <a href="" aria-label="Next">
50 <span aria-hidden="true">»</span>
51 </a>
52 </li>
53 {% endif %}
54
55 </ul>
56 </nav>
57 </body>
58 </html>