其他分享
首页 > 其他分享> > Django当中自己定义写一个分页器

Django当中自己定义写一个分页器

作者:互联网

在python当中自己写一个分页器,而不是直接运用Django中的分页器组件。

1.py

  1 """
  2 分页组件
  3 """
  4 
  5 
  6 class Pagination(object):
  7     def __init__(self, current_page, all_count, base_url, query_params, per_page=20, pager_page_count=11):
  8         """
  9         分页初始化
 10         :param current_page: 当前页码
 11         :param per_page: 每页显示数据条数
 12         :param all_count: 数据库中总条数
 13         :param base_url: 基础URL
 14         :param query_params: QueryDict对象,内部含所有当前URL的原条件
 15         :param pager_page_count: 页面上最多显示的页码数量
 16         """
 17         self.base_url = base_url
 18         try:
 19             self.current_page = int(current_page)
 20             if self.current_page <= 0:
 21                 raise Exception()
 22         except Exception as e:
 23             self.current_page = 1
 24         self.query_params = query_params
 25         self.per_page = per_page
 26         self.all_count = all_count
 27         self.pager_page_count = pager_page_count
 28         pager_count, b = divmod(all_count, per_page)
 29         if b != 0:
 30             pager_count += 1
 31         self.pager_count = pager_count
 32 
 33         half_pager_page_count = int(pager_page_count / 2)
 34         self.half_pager_page_count = half_pager_page_count
 35 
 36     @property
 37     def start(self):
 38         """
 39         数据获取值起始索引
 40         :return:
 41         """
 42         return (self.current_page - 1) * self.per_page
 43 
 44     @property
 45     def end(self):
 46         """
 47         数据获取值结束索引
 48         :return:
 49         """
 50         return self.current_page * self.per_page
 51 
 52     def page_html(self):
 53         """
 54         生成HTML页码
 55         :return:
 56         """
 57         # 如果数据总页码pager_count<11 pager_page_count
 58         if self.pager_count < self.pager_page_count:
 59             pager_start = 1
 60             pager_end = self.pager_count
 61         else:
 62             # 数据页码已经超过11
 63             # 判断: 如果当前页 <= 5 half_pager_page_count
 64             if self.current_page <= self.half_pager_page_count:
 65                 pager_start = 1
 66                 pager_end = self.pager_page_count
 67             else:
 68                 # 如果: 当前页+5 > 总页码
 69                 if (self.current_page + self.half_pager_page_count) > self.pager_count:
 70                     pager_end = self.pager_count
 71                     pager_start = self.pager_count - self.pager_page_count + 1
 72                 else:
 73                     pager_start = self.current_page - self.half_pager_page_count
 74                     pager_end = self.current_page + self.half_pager_page_count
 75 
 76         page_list = []
 77 
 78         if self.current_page <= 1:
 79             prev = '<li><a href="#">上一页</a></li>'
 80         else:
 81             self.query_params['page'] = self.current_page - 1
 82             prev = '<li><a href="%s?%s">上一页</a></li>' % (self.base_url, self.query_params.urlencode())
 83         page_list.append(prev)
 84         for i in range(pager_start, pager_end + 1):
 85             self.query_params['page'] = i
 86             if self.current_page == i:
 87                 tpl = '<li class="active"><a href="%s?%s">%s</a></li>' % (
 88                     self.base_url, self.query_params.urlencode(), i,)
 89             else:
 90                 tpl = '<li><a href="%s?%s">%s</a></li>' % (self.base_url, self.query_params.urlencode(), i,)
 91             page_list.append(tpl)
 92 
 93         if self.current_page >= self.pager_count:
 94             nex = '<li><a href="#">下一页</a></li>'
 95         else:
 96             self.query_params['page'] = self.current_page + 1
 97             nex = '<li><a href="%s?%s">下一页</a></li>' % (self.base_url, self.query_params.urlencode(),)
 98         page_list.append(nex)
 99         page_str = "".join(page_list)
100         return page_str

后端v1.py

 1 def changelist_view():
 2     data_list = self.UserInfo.objects.all()
 3     per_page_count = 10
 4     all_count = self.UserInfo.objects.all().count() # 计算该queryset当中对象总数
 5         query_params = request.GET.copy()  # 拷贝一份  去拿 page=1
 6         query_params._mutable = True    # 申明可以修改了
 7         pager = Pagination(
 8             current_page=request.GET.get('page'),    # page= 2 
 9             all_count=all_count,   
10             base_url=request.path_info,       # /stark/userinfo/list/
11             query_params =query_params,      
12             per_page=per_page_count,  #每页显示页数
13         )
14         data_list = data_list[pager.start:pager.end]        
15 
16     return render(request,'index.html',{  'pager':pager,})

前端index.html

    <div>
        <nav>
            <ul class="pagination">
                {{ pager.page_html|safe }}
            </ul>
        </nav>
    </div>

 

标签:count,定义,self,Django,current,query,pager,page,分页
来源: https://www.cnblogs.com/shaoyishi/p/16531546.html