自定义分页器

概述:功能简单实用,引用bootstrap样式之后,可直接在页面渲染,直接上代码

page.py

  1 import copy
  2 class Pagination(object):
  3 
  4     def __init__(self,current_page_num,all_count,request,per_page_num=2,pager_count=11):
  5         """
  6         封装分页相关数据
  7         :param current_page_num: 当前访问页的数字
  8         :param all_count:    分页数据中的数据总条数
  9         :param per_page_num: 每页显示的数据条数
 10         :param pager_count:  最多显示的页码个数
 11         """
 12         try:
 13             current_page_num = int(current_page_num)
 14         except Exception as e:
 15             current_page_num = 1
 16 
 17         if current_page_num <1:
 18             current_page_num = 1
 19 
 20         self.current_page_num = current_page_num
 21 
 22         self.all_count = all_count
 23         self.per_page_num = per_page_num
 24 
 25         # 实际总页码
 26         all_pager, tmp = divmod(all_count, per_page_num)
 27         if tmp:
 28             all_pager += 1
 29         self.all_pager = all_pager
 30 
 31 
 32         self.pager_count = pager_count
 33         self.pager_count_half = int((pager_count - 1) / 2)  # 5
 34 
 35 
 36         # 保存搜索条件
 37         self.params=copy.deepcopy(request.GET) # {"a":"1","b":"2"}
 38 
 39     @property
 40     def start(self):
 41         return (self.current_page_num - 1) * self.per_page_num
 42 
 43     @property
 44     def end(self):
 45         return self.current_page_num * self.per_page_num
 46 
 47     def page_html(self):
 48         # 如果总页码 < 11个:
 49         if self.all_pager <= self.pager_count:
 50             pager_start = 1
 51             pager_end = self.all_pager + 1
 52         # 总页码  > 11
 53         else:
 54             # 当前页如果<=页面上最多显示11/2个页码
 55             if self.current_page_num <= self.pager_count_half:
 56                 pager_start = 1
 57                 pager_end = self.pager_count + 1
 58             # 当前页大于5
 59             else:
 60                 # 页码翻到最后
 61                 if (self.current_page_num + self.pager_count_half) > self.all_pager:
 62 
 63                     pager_start = self.all_pager - self.pager_count + 1
 64                     pager_end = self.all_pager + 1
 65 
 66                 else:
 67                     pager_start = self.current_page_num - self.pager_count_half
 68                     pager_end = self.current_page_num + self.pager_count_half + 1
 69 
 70         page_html_list = []
 71         self.params['page'] = 1
 72         first_page = '<li><a href="?%s">首页</a></li>' % (self.params.urlencode(),)
 73         page_html_list.append(first_page)
 74 
 75         if self.current_page_num <= 1:
 76             prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
 77         else:
 78             self.params['page'] = self.current_page_num - 1
 79             prev_page = '<li><a href="?%s">上一页</a></li>' % (self.params.urlencode(),)
 80 
 81         page_html_list.append(prev_page)
 82 
 83         for i in range(pager_start, pager_end):
 84 
 85             self.params["page"]=i
 86 
 87             if i == self.current_page_num:
 88                 temp = '<li class="active"><a href="?%s">%s</a></li>' %(self.params.urlencode(),i)
 89             else:
 90                 temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(),i,)
 91             page_html_list.append(temp)
 92 
 93         if self.current_page_num >= self.all_pager:
 94             next_page = '<li class="disabled"><a href="#">下一页</a></li>'
 95         else:
 96             self.params['page'] = self.current_page_num+1
 97             next_page = '<li><a href="?%s">下一页</a></li>' % (self.params.urlencode(),)
 98         page_html_list.append(next_page)
 99         self.params['page'] = self.all_pager
100         last_page = '<li><a href="?%s">尾页</a></li>' % (self.params.urlencode(),)
101         page_html_list.append(last_page)
102 
103         return ''.join(page_html_list)

page.html

<nav aria-label="Page navigation" class="pull-right">
        <ul class="pagination">
              {{ pagination.page_html|safe }}
          </ul>
</nav>

这样简单的分页功能就实现啦!

原文地址:https://www.cnblogs.com/fengchong/p/9956944.html