Python分页组件
分页组件的实现:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | class Pagination( object ): """ 自定义分页 """ def __init__( self ,current_page,total_count,base_url,params,per_page_count = 10 ,max_pager_count = 11 ): try : current_page = int (current_page) except Exception as e: current_page = 1 if current_page < = 0 : current_page = 1 self .current_page = current_page # 数据总条数 self .total_count = total_count # 每页显示10条数据 self .per_page_count = per_page_count # 页面上应该显示的最大页码 max_page_num, div = divmod (total_count, per_page_count) if div: max_page_num + = 1 self .max_page_num = max_page_num # 页面上默认显示11个页码(当前页在中间) self .max_pager_count = max_pager_count self .half_max_pager_count = int ((max_pager_count - 1 ) / 2 ) # URL前缀 self .base_url = base_url # request.GET import copy params = copy.deepcopy(params) params._mutable = True # 包含当前列表页面所有的搜索条件 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # self.params[page] = 8 # self.params.urlencode() # source=2&status=2&gender=2&consultant=1&page=8 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" # href="%s?%s" %(self.base_url,self.params.urlencode()) self .params = params @property 这个 property 的作用是,在调用的时候不用加上括号,直接使用 self .start即可 def start( self ): return ( self .current_page - 1 ) * self .per_page_count @property def end( self ): return self .current_page * self .per_page_count def page_html( self ): # 如果总页数 <= 11 if self .max_page_num < = self .max_pager_count: pager_start = 1 pager_end = self .max_page_num # 如果总页数 > 11 else : # 如果当前页 <= 5 if self .current_page < = self .half_max_pager_count: pager_start = 1 pager_end = self .max_pager_count else : # 当前页 + 5 > 总页码 if ( self .current_page + self .half_max_pager_count) > self .max_page_num: pager_end = self .max_page_num pager_start = self .max_page_num - self .max_pager_count + 1 #倒这数11个 else : pager_start = self .current_page - self .half_max_pager_count pager_end = self .current_page + self .half_max_pager_count page_html_list = [] # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} # 首页 self .params[ 'page' ] = 1 first_page = '<li><a href="%s?%s">首页</a></li>' % ( self .base_url, self .params.urlencode(),) page_html_list.append(first_page) # 上一页 self .params[ "page" ] = self .current_page - 1 if self .params[ "page" ] < 1 : pervious_page = '<li class="disabled"><a href="%s?%s" aria-label="Previous">上一页</span></a></li>' % ( self .base_url, self .params.urlencode()) else : pervious_page = '<li><a href = "%s?%s" aria-label = "Previous" >上一页</span></a></li>' % ( self .base_url, self .params.urlencode()) page_html_list.append(pervious_page) # 中间页码 for i in range (pager_start, pager_end + 1 ): self .params[ 'page' ] = i if i = = self .current_page: temp = '<li class="active"><a href="%s?%s">%s</a></li>' % ( self .base_url, self .params.urlencode(), i,) else : temp = '<li><a href="%s?%s">%s</a></li>' % ( self .base_url, self .params.urlencode(), i,) page_html_list.append(temp) # 下一页 self .params[ "page" ] = self .current_page + 1 if self .params[ "page" ] > self .max_page_num: self .params[ "page" ] = self .current_page next_page = '<li class="disabled"><a href = "%s?%s" aria-label = "Next">下一页</span></a></li >' % ( self .base_url, self .params.urlencode()) else : next_page = '<li><a href = "%s?%s" aria-label = "Next">下一页</span></a></li>' % ( self .base_url, self .params.urlencode()) page_html_list.append(next_page) # 尾页 self .params[ 'page' ] = self .max_page_num last_page = '<li><a href="%s?%s">尾页</a></li>' % ( self .base_url, self .params.urlencode(),) page_html_list.append(last_page) return ''.join(page_html_list) |
分页组件的使用
1 2 3 4 5 6 7 | """ 自定义分页组件的使用方法: pager_obj = Pagination(request.GET.get('page',1),len(HOST_LIST),request.path_info,request.GET) host_list = HOST_LIST[pager_obj.start:pager_obj.end] html = pager_obj.page_html() ----->这里返回的是page_html_list return render(request,'hosts.html',{'host_list':host_list,"page_html":html}) """ |
按照上面我们在前端之间传入数据到py文件中
1 | pager_obj = Pagination(request.GET.get( 'page' , 1 ), len (HOST_LIST),request.path_info,request.GET) |
1 | request.GET.get( 'page' , 1 ) - - - - - - 》 current_page |
1 | len (HOST_LIST) - - - - - - 》 total_count(这里要是数据库的话,我们可以取总条数) |
1 | request.path_info - - - - - - 》 base_Url |
1 | request.GET - - - - - - 》 params |
原文章转自:
http://www.cnblogs.com/haiyan123/p/8065353.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架