Django 分页器的使用

在我们做Django项目,数据量比较大的时候,我们需要分页器(paginator)来做分页显示,效果如下

 

实现方法如下:

在视图函数views.py中,引入Paginator

1 from django.core.paginator import Paginator

首先在这里说一下分页器的主要方法:

1 1.整个数据表
2      paginator.count   数据总数
3      paginator.num_pages   总页数
4      paginator.page_range   页码的列表
5 2.当前页
6      curuent_page.has_next()   是否有下一页
7      curuent_page.next_page_number()   下一页的页码
8      curuent_page.has_previous()   是否有上一页
9      curuent_page.previous_page_number()   上一页的页码

下面是具体实现的代码:

1.views.py

 1 from django.shortcuts import render,HttpResponse,redirect
 2 
 3 from app01.models import * #应用数据库表
 4 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger #导入分页器
 5 
 6 def demo(requerst):
 7 
 8     book_list = Book.objects.all()  #获取所有的数据
 9 
10     paginator = Paginator(book_list,3)#分别是需要分页的数据 和多少行数据为一页
11     pag_num = paginator.num_pages#获取整个表的总页数
12     curuent_page_num = int(requerst.GET.get('page',1))#获取当前页数,默认为1
13     curuent_page = paginator.page(curuent_page_num)#获取当前页的数据
14 
15 
16     if pag_num<11:#判断当前页是否小于11个
17         pag_range = paginator.page_range
18     elif pag_num>11:
19         if curuent_page_num<6:
20             pag_range = range(1,11)
21         elif curuent_page_num>(paginator.num_pages)-5:
22             pag_range = range(pag_num-9, pag_num+1)
23         else:
24             pag_range = range(curuent_page_num-5,curuent_page_num+5) #当前页+5大于最大页数时
25 
26 
27 
28 
29     return render(requerst,"demo.html",{'book_list' :book_list,"pagintor" : paginator,"current_Page":curuent_page,"current_Page_num":curuent_page_num,"pag_range":pag_range})

2.demo.html

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>分页器</title>
 6     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">#引入bootstrap
 7 
 8 </head>
 9 <body style="margin-top: 100px">
10     <div class="container">
11         <div class="row">
12             <div class="col-xs-6 col-xs-offset-3 col-md-6 col-md-offset-3">
13                 <div class="panel panel-default">
14                     <!-- Table -->
15                     <table class="table table-striped table-bordered table-hover">
16                         <thead>
17                             <tr>
18                                 <th>书籍编号</th>
19                                 <th>书籍名称</th>
20                                 <th>书籍价格</th>
21                             </tr>
22                         </thead>
23                         <tbody>
24                             {% for book in current_Page %}
25                                 <tr>
26                                     <td>{{ book.pk }}</td>
27                                     <td>{{ book.title }}</td>
28                                     <td>{{ book.price }}</td>
29                                 </tr>
30                             {% endfor %}
31 
32                         </tbody>
33                     </table>
34                 </div>
35 
36 
37                 <div>
38                     <nav aria-label="Page navigation">
39                       <ul class="pagination">
40                             {% if not current_Page.has_previous %}<!--判断是否有上一页-->
41                                 <li class="disable">
42                                   <a href="#" aria-label="Previous">
43                                     <span aria-hidden="true">上一页</span>
44                                   </a>
45                                 </li>
46                                 {% else %}
47                                 <li>
48                                   <a href="?page={{ current_Page.previous_page_number }}" aria-label="Previous">
49                                     <span aria-hidden="true">上一页</span>
50                                   </a>
51                                 </li>
52                             {% endif %}
53 
54                           {% for page_range in pag_range %}
55                               {% if current_Page_num == page_range %}<!--判断遍历的页数是否为当前页,是就添加.avtive 背景色变蓝-->
56                                     <li class="active"><a href="?page={{ page_range }}">{{ page_range }}</a></li>
57                               {% else %}
58                                     <li><a href="?page={{ page_range }}">{{ page_range }}</a></li>
59                               {% endif %}
60                           {% endfor %}
61 
62                             {% if not current_Page.has_next %}<!-- 判断是否最后一页 -->
63                                 <li class="disable">
64                                   <a href="?page={{ current_Page_num }}" aria-label="Next">
65                                     <span aria-hidden="true">下一页</span>
66                                   </a>
67                                 </li>
68                                 {% else %}
69                                 <li>
70                                   <a href="?page={{ current_Page.next_page_number }}" aria-label="Next">
71                                     <span aria-hidden="true">下一页</span>
72                                   </a>
73                                 </li>
74                             {% endif %}
75 
76                       </ul>
77                     </nav>
78                 </div>
79             </div>
80         </div>
81     </div>
82 </body>
83 </html>

最后,把路由转换设置正确,即可

例如:

1 path('demo/',views.demo)

分页器就这样完成了!!!

猜你喜欢

转载自www.cnblogs.com/lhb-alan/p/11355619.html