1 """ 2 自定义分页组件的使用方法: 3 pager_obj = Pagination(request.GET.get(‘page‘,1),len(HOST_LIST),request.path_info,request.GET) 4 host_list = HOST_LIST[pager_obj.start:pager_obj.end] 5 html = pager_obj.page_html() 6 return render(request,‘hosts.html‘,{‘host_list‘:host_list,"page_html":html}) 7 """ 8 """ 9 10 <!DOCTYPE html> 11 <html lang="en"> 12 <head> 13 <meta charset="UTF-8"> 14 <title>Title</title> 15 <style> 16 .pager a{ 17 display: inline-block; 18 padding: 2px 8px; 19 margin: 0 5px; 20 border: 1px solid cadetblue; 21 } 22 .pager a.active{ 23 background-color: #2b669a; 24 color: white; 25 } 26 </style> 27 </head> 28 <body> 29 <h1>用户列表</h1> 30 <ul> 31 {% for item in user_list %} 32 <li>{{ item }}</li> 33 {% endfor %} 34 </ul> 35 <div class="pager"> 36 {{ page_html|safe }} 37 </div> 38 </body> 39 </html> 40 """ 41 42 class Pagination(object): 43 """ 44 自定义分页 45 """ 46 def __init__(self,current_page,total_count,base_url,params,per_page_count=10,max_pager_count=11): 47 try: 48 current_page = int(current_page) 49 except Exception as e: 50 current_page = 1 51 if current_page <=0: 52 current_page = 1 53 self.current_page = current_page 54 # 数据总条数 55 self.total_count = total_count 56 57 # 每页显示10条数据 58 self.per_page_count = per_page_count 59 60 # 页面上应该显示的最大页码 61 max_page_num, div = divmod(total_count, per_page_count) 62 if div: 63 max_page_num += 1 64 self.max_page_num = max_page_num 65 66 # 页面上默认显示11个页面(当前页在中间) 67 self.max_pager_count = max_pager_count 68 self.half_max_pager_count = int((max_pager_count - 1) / 2) 69 70 # URL前缀 71 self.base_url = base_url 72 73 # request.GET 74 import copy 75 params = copy.deepcopy(params) 76 params._mutable = True 77 # 包含当前列表页面所有的搜索条件 78 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} 79 # self.params[page] = 8 80 # self.params.urlencode() 81 # source=2&status=2&gender=2&consultant=1&page=8 82 # href="/hosts/?source=2&status=2&gender=2&consultant=1&page=8" 83 # href="%s?%s" %(self.base_url,self.params.urlencode()) 84 self.params = params 85 86 @property 87 def start(self): 88 return (self.current_page - 1) * self.per_page_count 89 90 @property 91 def end(self): 92 return self.current_page * self.per_page_count 93 94 def page_html(self): 95 # 如果总页数 <= 11 96 if self.max_page_num <= self.max_pager_count: 97 pager_start = 1 98 pager_end = self.max_page_num 99 # 如果总页数 > 11 100 else: 101 # 如果当前页 <= 5 102 if self.current_page <= self.half_max_pager_count: 103 pager_start = 1 104 pager_end = self.max_pager_count 105 else: 106 # 当前页 + 5 > 总页码 107 if (self.current_page + self.half_max_pager_count) > self.max_page_num: 108 pager_end = self.max_page_num 109 pager_start = self.max_page_num - self.max_pager_count + 1 110 else: 111 pager_start = self.current_page - self.half_max_pager_count 112 pager_end = self.current_page + self.half_max_pager_count 113 114 page_html_list = [] 115 # {source:[2,], status:[2], gender:[2],consultant:[1],page:[1]} 116 self.params[‘page‘] = 1 117 first_page = ‘<a href="%s?%s">首页</a>‘ % (self.base_url,self.params.urlencode(),) 118 page_html_list.append(first_page) 119 # 上一页 120 121 for i in range(pager_start, pager_end + 1): 122 self.params[‘page‘] = i 123 if i == self.current_page: 124 temp = ‘<a class="active" href="%s?%s">%s</a>‘ % (self.base_url,self.params.urlencode(), i,) 125 else: 126 temp = ‘<a href="%s?%s">%s</a>‘ % (self.base_url,self.params.urlencode(), i,) 127 page_html_list.append(temp) 128 129 # 下一页 130 131 self.params[‘page‘] = self.max_page_num 132 last_page = ‘<a href="%s?%s">尾页</a>‘ % (self.base_url, self.params.urlencode(),) 133 page_html_list.append(last_page) 134 135 return ‘‘.join(page_html_list)