• 分页器组件支持保存搜索条件


    代码:

      1 class Pagination(object):
      2     def __init__(self, current_page, all_count, params,per_page_num=10, pager_count=11):
      3         """
      4         封装分页相关数据
      5         :param current_page: 当前页
      6         :param all_count:    数据库中的数据总条数
      7         :param per_page_num: 每页显示的数据条数
      8         :param pager_count:  最多显示的页码个数
      9 
     10         用法:
     11         queryset = model.objects.all()
     12         page_obj = Pagination(current_page,all_count)
     13         page_data = queryset[page_obj.start:page_obj.end]
     14         获取数据用page_data而不再使用原始的queryset
     15         获取前端分页样式用page_obj.page_html
     16 
     17         示例代码:
     18         current_page = self.request.GET.get("page", 1)
     19         self.page_obj = Pagination(current_page=current_page,all_count=self.queryset.count(),params=self.request.GET)
     20         self.page_queryset = self.queryset[self.page_obj.start:self.page_obj.end]
     21         前台:{{ show_obj.page_obj.page_html|safe }}
     22         """
     23         try:
     24             current_page = int(current_page)
     25         except Exception as e:
     26             current_page = 1
     27 
     28         if current_page < 1:
     29             current_page = 1
     30 
     31         self.current_page = current_page
     32 
     33         self.all_count = all_count
     34         self.per_page_num = per_page_num
     35 
     36         # 总页码
     37         all_pager, tmp = divmod(all_count, per_page_num)
     38         if tmp:
     39             all_pager += 1
     40         self.all_pager = all_pager
     41 
     42         self.pager_count = pager_count
     43         self.pager_count_half = int((pager_count - 1) / 2)
     44 
     45         import copy
     46         self.params = copy.deepcopy(params)
     47 
     48     @property
     49     def start(self):
     50         return (self.current_page - 1) * self.per_page_num
     51 
     52     @property
     53     def end(self):
     54         return self.current_page * self.per_page_num
     55 
     56     def page_html(self):
     57         # 如果总页码 < 11个:
     58         if self.all_pager <= self.pager_count:
     59             pager_start = 1
     60             pager_end = self.all_pager + 1
     61         # 总页码  > 11
     62         else:
     63             # 当前页如果<=页面上最多显示11/2个页码
     64             if self.current_page <= self.pager_count_half:
     65                 pager_start = 1
     66                 pager_end = self.pager_count + 1
     67 
     68             # 当前页大于5
     69             else:
     70                 # 页码翻到最后
     71                 if (self.current_page + self.pager_count_half) > self.all_pager:
     72                     pager_end = self.all_pager + 1
     73                     pager_start = self.all_pager - self.pager_count + 1
     74                 else:
     75                     pager_start = self.current_page - self.pager_count_half
     76                     pager_end = self.current_page + self.pager_count_half + 1
     77 
     78         page_html_list = []
     79         # 添加前面的nav和ul标签
     80         page_html_list.append('''
     81                     <nav aria-label='Page navigation>'
     82                     <ul class='pagination'>
     83                 ''')
     84         first_page = '<li><a href="?page=%s">首页</a></li>' % (1)
     85         page_html_list.append(first_page)
     86 
     87         if self.current_page <= 1:
     88             prev_page = '<li class="disabled"><a href="#">上一页</a></li>'
     89         else:
     90             prev_page = '<li><a href="?page=%s">上一页</a></li>' % (self.current_page - 1,)
     91 
     92         page_html_list.append(prev_page)
     93 
     94         for i in range(pager_start, pager_end):
     95             self.params['page'] = i
     96             if i == self.current_page:
     97                 temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.params.urlencode(), i,)
     98             else:
     99                 temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(), i,)
    100             page_html_list.append(temp)
    101 
    102         if self.current_page >= self.all_pager:
    103             next_page = '<li class="disabled"><a href="#">下一页</a></li>'
    104         else:
    105             next_page = '<li><a href="?page=%s">下一页</a></li>' % (self.current_page + 1,)
    106         page_html_list.append(next_page)
    107 
    108         last_page = '<li><a href="?page=%s">尾页</a></li>' % (self.all_pager,)
    109         page_html_list.append(last_page)
    110         # 尾部添加标签
    111         page_html_list.append('''
    112                                            </nav>
    113                                            </ul>
    114                                        ''')
    115         return ''.join(page_html_list)
    View Code
  • 相关阅读:
    mybatis总结
    常用问题总结
    FreeMarker学习(springmvc配置)
    Jupyter Notebook 添加目录
    Cousera 无法播放视频 解决办法 widows 和 linux
    python 列表 字典 读写文件:pickle模块的基本使用
    python numpy 使用笔记 矩阵操作
    Linux 下 将使用Python-Django开发的web应用布置到服务器上(亲测有效)
    ORACLE数据库操作基本语句
    Linux 下安装Python框架django建立与mysql的连接
  • 原文地址:https://www.cnblogs.com/xuechengeng/p/10769488.html
Copyright © 2020-2023  润新知