Django分页

分页

 1 from django.shortcuts import render
 2 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 3 
 4 L = []
 5 for i in range(999):
 6     L.append(i)
 7 
 8 def index(request):
 9     current_page = request.GET.get('p')
10 
11     paginator = Paginator(L, 10)
12     # per_page: 每页显示条目数量
13     # count:    数据总个数
14     # num_pages:总页数
15     # page_range:总页数的索引范围,如: (1,10),(1,200)
16     # page:     page对象
17     try:
18         posts = paginator.page(current_page)
19         # has_next              是否有下一页
20         # next_page_number      下一页页码
21         # has_previous          是否有上一页
22         # previous_page_number  上一页页码
23         # object_list           分页之后的数据列表
24         # number                当前页
25         # paginator             paginator对象
26     except PageNotAnInteger:
27         posts = paginator.page(1)
28     except EmptyPage:
29         posts = paginator.page(paginator.num_pages)
30     return render(request, 'index.html', {'posts': posts})
Django内置分页view函数
 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8 <ul>
 9     {% for item in posts %}
10         <li>{{ item }}</li>
11     {% endfor %}
12 </ul>
13 
14 <div class="pagination">
15       <span class="step-links">
16         {% if posts.has_previous %}
17             <a href="?p={{ posts.previous_page_number }}">Previous</a>
18         {% endif %}
19           <span class="current">
20             Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
21           </span>
22           {% if posts.has_next %}
23               <a href="?p={{ posts.next_page_number }}">Next</a>
24           {% endif %}
25       </span>
26 
27 </div>
28 </body>
29 </html>
Django内置分页HTML页面
 1 from django.shortcuts import render
 2 from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
 3 
 4 
 5 class CustomPaginator(Paginator):
 6     def __init__(self, current_page, max_pager_num, *args, **kwargs):
 7         """
 8         :param current_page: 当前页
 9         :param max_pager_num:最多显示的页码个数
10         :param args:
11         :param kwargs:
12         :return:
13         """
14         self.current_page = int(current_page)
15         self.max_pager_num = max_pager_num
16         super(CustomPaginator, self).__init__(*args, **kwargs)
17 
18     def page_num_range(self):
19         # 当前页面
20         # self.current_page
21         # 总页数
22         # self.num_pages
23         # 最多显示的页码个数
24         # self.max_pager_num
25         print(1)
26         if self.num_pages < self.max_pager_num:
27             return range(1, self.num_pages + 1)
28         print(2)
29         part = int(self.max_pager_num / 2)
30         if self.current_page - part < 1:
31             return range(1, self.max_pager_num + 1)
32         print(3)
33         if self.current_page + part > self.num_pages:
34             return range(self.num_pages + 1 - self.max_pager_num, self.num_pages + 1)
35         print(4)
36         return range(self.current_page - part, self.current_page + part + 1)
37 
38 
39 L = []
40 for i in range(999):
41     L.append(i)
42 
43 def index(request):
44     current_page = request.GET.get('p')
45     paginator = CustomPaginator(current_page, 11, L, 10)
46     # per_page: 每页显示条目数量
47     # count:    数据总个数
48     # num_pages:总页数
49     # page_range:总页数的索引范围,如: (1,10),(1,200)
50     # page:     page对象
51     try:
52         posts = paginator.page(current_page)
53         # has_next              是否有下一页
54         # next_page_number      下一页页码
55         # has_previous          是否有上一页
56         # previous_page_number  上一页页码
57         # object_list           分页之后的数据列表
58         # number                当前页
59         # paginator             paginator对象
60     except PageNotAnInteger:
61         posts = paginator.page(1)
62     except EmptyPage:
63         posts = paginator.page(paginator.num_pages)
64 
65     return render(request, 'index.html', {'posts': posts})
扩展内置分页view函数
 1 <!DOCTYPE html>
 2 <html>
 3 <head lang="en">
 4     <meta charset="UTF-8">
 5     <title></title>
 6 </head>
 7 <body>
 8 
 9 <ul>
10     {% for item in posts %}
11         <li>{{ item }}</li>
12     {% endfor %}
13 </ul>
14 
15 <div class="pagination">
16 <span class="step-links">
17 {% if posts.has_previous %}
18     <a href="?p={{ posts.previous_page_number }}">Previous</a>
19 {% endif %}
20 
21     {% for i in posts.paginator.page_num_range %}
22         <a href="?p={{ i }}">{{ i }}</a>
23     {% endfor %}
24 
25     {% if posts.has_next %}
26         <a href="?p={{ posts.next_page_number }}">Next</a>
27     {% endif %}
28 </span>
29 
30 <span class="current">
31 Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
32 </span>
33 
34 </div>
35 </body>
36 </html>
扩展内置分页html页面
 1 __author__ = 'Administrator'
 2 from django.utils.safestring import mark_safe
 3 
 4 
 5 class Pagination(object):
 6     def __init__(self, current_page, data_count, per_page_count=7, pager_num=7):
 7         try:
 8             self.current_page = int(current_page)
 9         except Exception as e:
10             self.current_page = 1
11         self.data_count = data_count
12         self.per_page_count = per_page_count
13         self.pager_num = pager_num
14 
15     @property
16     def start(self):
17         return (self.current_page - 1) * self.per_page_count
18 
19     @property
20     def end(self):
21         return self.current_page * self.per_page_count
22 
23     @property
24     def total_count(self):
25         v, y = divmod(self.data_count, self.per_page_count)
26         if y:
27             v += 1
28         return v
29 
30     def page_str(self, base_url):
31         page_list = []
32 
33         if self.total_count < self.pager_num:
34             start_index = 1
35             end_index = self.total_count + 1
36         else:
37             if self.current_page <= (self.pager_num + 1) / 2:
38                 start_index = 1
39                 end_index = self.pager_num + 1
40             else:
41                 start_index = self.current_page - (self.pager_num - 1) / 2
42                 end_index = self.current_page + (self.pager_num + 1) / 2
43                 if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
44                     end_index = self.total_count + 1
45                     start_index = self.total_count - self.pager_num + 1
46 
47         if self.current_page == 1:
48             prev = '<li><a class="page" href="javascript:void(0);">上一页</a></li>'
49         else:
50             prev = '<li><a class="page" href="%s?p=%s">上一页</a></li>' % (base_url, self.current_page - 1,)
51         page_list.append(prev)
52 
53         for i in range(int(start_index), int(end_index)):
54             if i == self.current_page:
55                 temp = '<li class="active"><a class="page active" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
56             else:
57                 temp = '<li><a class="page" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
58             page_list.append(temp)
59 
60         if self.current_page == self.total_count:
61             nex = '<li><a class="page" href="javascript:void(0);">下一页</a></li>'
62         else:
63             nex = '<li><a class="page" href="%s?p=%s">下一页</a></li>' % (base_url, self.current_page + 1,)
64         page_list.append(nex)
65 
66         # jump = """
67         # <input type='text'  /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
68         # <script>
69         #     function jumpTo(ths,base){
70         #         var val = ths.previousSibling.value;
71         #         location.href = base + val;
72         #     }
73         # </script>
74         # """ % (base_url,)
75         #
76         # page_list.append(jump)
77 
78         page_str = mark_safe("".join(page_list))
79 
80         return page_str
自定义分页组件

 

posted @ 2020-03-12 17:35  佛祖让我来巡山  阅读(101)  评论(0编辑  收藏  举报

佛祖让我来巡山博客站 - 创建于 2018-08-15

开发工程师个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

Bootstrap中文网