Django中的分页

Posted zhwang1216

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django中的分页相关的知识,希望对你有一定的参考价值。

方法一:
views
技术图片
 1 def books(request):
 2     # url取page参数书
 3     page_num = request.GET.get(page)
 4     print(page_num, type(page_num))
 5     # 总页码数
 6     total_count = models.Book.objects.all().count()
 7     # 每页显示多少条数据
 8     per_page = 10
 9     # 总共需要多少页码展示
10     total_page, m = divmod(total_count, per_page)
11     if m:
12         total_page += 1
13 
14     try:
15         page_num = int(page_num)
16         if page_num > total_page:
17             page_num = total_page
18     except Exception as e:
19         # 页码不是数字,默认返回第一页
20         page_num = 1
21 
22     # 定义两个变量保存数据从取到哪儿
23     data_start = (page_num - 1) * 10
24     data_end = page_num * 10
25 
26     # 页面上最多展示多少个页码
27     max_page = 11
28     if total_page < max_page:
29         max_page = total_page
30 
31     half_max_page = max_page // 2
32     # 页码上展示的页码从哪儿开始
33     page_start = page_num - half_max_page
34     # 页面展示到哪儿结束
35     page_end = page_num + half_max_page
36     # 如果当前页面减一半,比1还小
37     if page_start <= 1:
38         page_start = 1
39         page_end = max_page
40     # 如果当前页 加 一半 比总页码数还大
41     if page_end >= total_page:
42         page_end = total_page
43         page_start = total_page - max_page
44 
45     all_book = models.Book.objects.all()[data_start:data_end]
46     # 自己拼接分页的代码
47     html_str_list = []
48     # 加上第一页(首页)
49     html_str_list.append(<li><a href="/books/?page=1">首页</a></li>)
50 
51     # 判断一下,如果是第一页,就没有上一页
52     if page_num <= 1:
53         html_str_list.append(
54             <li class="disabled"><a href="#"> <span aria-hidden="true">&laquo;</span></a></li>.format(
55                 page_num - 1))
56     else:
57         html_str_list.append(
58             <li><a href="/books/?page={}"> <span aria-hidden="true">&laquo;</span></a></li>.format(page_num - 1))
59 
60     for i in range(page_start, page_end + 1):
61         tmp = <li> <a href="/books/?page={0}">{0}</a></li>.format(i)
62         html_str_list.append(tmp)
63     # 加一个下一页的标签(下一页)
64     # 判断是否是最后一页
65     if page_num >= total_page:
66         html_str_list.append(
67             <li class="disabled" ><a href="#"> <span aria-hidden="true">&raquo;</span></a></li>)
68     else:
69         html_str_list.append(
70             <li><a href="/books/?page={}"> <span aria-hidden="true">&raquo;</span></a></li>.format(page_num + 1))
71 
72     # 加上最后一页(尾页)
73     html_str_list.append(<li><a href="/books/?page={}">尾页</a></li>.format(total_page))
74 
75     page_html = "".join(html_str_list)
76     return render(request, "books.html",
77                   {books: all_book, "page_html": page_html})
View Code
book.HTML
技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>书籍列表</title>
 6     <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
 7 
 8 </head>
 9 <body>
10 <div class="container">
11     <table class="table table-bordered">
12         <thead>
13         <tr>
14             <th>序号</th>
15             <th>ID</th>
16             <th>书名</th>
17         </tr>
18         </thead>
19         <tbody>
20         {% for book in books %}
21             <tr>
22                 <td>{{ forloop.counter }}</td>
23                 <td>{{ book.id }}</td>
24                 <td>{{ book.title }}</td>
25             </tr>
26         {% endfor %}
27         </tbody>
28     </table>
29     <nav aria-label="Page navigation">
30         <ul class="pagination">
31             <li>
32                     {{ page_html|safe }}
33             </li>
34         </ul>
35     </nav>
36 </div>
37 </body>
38 </html>
View Code

 方法二:利用封装

创建文件utils文件

mypage

技术图片
 1 class Page():
 2 
 3     def __init__(self, page_num, total_count, url_prefix, per_page=10, max_page=11):
 4         """
 5 
 6         :param page_num: 当前页码数
 7         :param total_count: 数据总数
 8         :param url_prefix: a标签href的前缀
 9         :param per_page: 每页显示多少条数据
10         :param max_page: 页面上最多显示几个页码
11         """
12         self.url_prefix = url_prefix
13         self.max_page = max_page
14         # 每一页显示多少条数据
15         # 总共需要多少页码来展示
16         total_page, m = divmod(total_count, per_page)
17         if m:
18             total_page += 1
19         self.total_page = total_page
20 
21         try:
22             page_num = int(page_num)
23             # 如果输入的页码数超过了最大的页码数,默认返回最后一页
24             if page_num > total_page:
25                 page_num = total_page
26         except Exception as e:
27             # 当输入的页码不是正经数字的时候 默认返回第一页的数据
28             page_num = 1
29         self.page_num = page_num
30 
31         # 定义两个变量保存数据从哪儿取到哪儿
32         self.data_start = (page_num - 1) * 10
33         self.data_end = page_num * 10
34 
35         # 页面上总共展示多少页码
36         if total_page < self.max_page:
37             self.max_page = total_page
38 
39         half_max_page = self.max_page // 2
40         # 页面上展示的页码从哪儿开始
41         page_start = page_num - half_max_page
42         # 页面上展示的页码到哪儿结束
43         page_end = page_num + half_max_page
44         # 如果当前页减一半 比1还小
45         if page_start <= 1:
46             page_start = 1
47             page_end = self.max_page
48         # 如果 当前页 加 一半 比总页码数还大
49         if page_end >= total_page:
50             page_end = total_page
51             page_start = total_page - self.max_page + 1
52         self.page_start = page_start
53         self.page_end = page_end
54 
55     @property
56     def start(self):
57         return self.data_start
58 
59     @property
60     def end(self):
61         return self.data_end
62 
63 
64     def page_html(self):
65         # 自己拼接分页的HTML代码
66         html_str_list = []
67         # 加上第一页
68         html_str_list.append(<li><a href="{}?page=1">首页</a></li>.format( self.url_prefix))
69 
70         # 判断一下 如果是第一页,就没有上一页
71         if self.page_num <= 1:
72             html_str_list.append(<li class="disabled"><a href="#"><span aria-hidden="true">&laquo;</span></a></li>.format(self.page_num-1))
73         else:
74             # 加一个上一页的标签
75             html_str_list.append(<li><a href="{}?page={}"><span aria-hidden="true">&laquo;</span></a></li>.format( self.url_prefix, self.page_num-1))
76 
77         for i in range(self.page_start, self.page_end+1):
78             # 如果是当前页就加一个active样式类
79             if i == self.page_num:
80                 tmp = <li class="active"><a href="{0}?page={1}">{1}</a></li>.format(self.url_prefix, i)
81             else:
82                 tmp = <li><a href="{0}?page={1}">{1}</a></li>.format( self.url_prefix, i)
83 
84             html_str_list.append(tmp)
85 
86         # 加一个下一页的按钮
87         # 判断,如果是最后一页,就没有下一页
88         if self.page_num >= self.total_page:
89             html_str_list.append(<li class="disabled"><a href="#"><span aria-hidden="true">&raquo;</span></a></li>)
90         else:
91             html_str_list.append(<li><a href="{}?page={}"><span aria-hidden="true">&raquo;</span></a></li>.format( self.url_prefix, self.page_num+1))
92         # 加最后一页
93         html_str_list.append(<li><a href="{}?page={}">尾页</a></li>.format( self.url_prefix, self.total_page))
94 
95         page_html = "".join(html_str_list)
96         return page_html
封装代码

views

技术图片
 1 def depts(request):
 2     page_num = request.GET.get(page)
 3     print(page_num, type(page_num))
 4     # 总页码
 5     total_count = models.Dept.objects.all().count()
 6     from utils.mypage import Page
 7     page_obj = Page(page_num, total_count, per_page=10, url_prefix="/depts/", max_page=11, )
 8     ret = models.Book.objects.all()[page_obj.start:page_obj.end]
 9     print(ret)
10     page_html = page_obj.page_html()
11     return render(request,"dept.html",{dept:ret,page_html:page_html})
视图

html代码

技术图片
 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5      <title>部门列表</title>
 6     <link rel="stylesheet" href="/static/bootstrap/css/bootstrap.min.css">
 7 </head>
 8 <body>
 9 
10     <div class="container">
11         <table class="table table-bordered">
12           <thead>
13             <tr>
14                 <th>序号</th>
15                 <th>id</th>
16                 <th>部门名</th>
17             </tr>
18           </thead>
19             <tbody>
20             {% for dept in depts %}
21                 <tr>
22                 <td>{{ forloop.counter }}</td>
23                 <td>{{ dept.id }}</td>
24                 <td>{{ dept.name }}</td>
25                 </tr>
26             {% endfor %}
27 
28             </tbody>
29         </table>
30         <nav aria-label="Page navigation">
31           <ul class="pagination">
32                 {{ page_html|safe }}
33           </ul>
34         </nav>
35     </div>
36 </body>
View Code

 

以上是关于Django中的分页的主要内容,如果未能解决你的问题,请参考以下文章

django中的分页

django 中的分页 - 原始查询字符串丢失

Django中的分页概念和实现大型对象的有效方法

django中的分页设置

Django中的分页

Django中的分页器以及手绘验证码