自定义分页(类封装高级版+函数低级版)
类封装高级版:
1 #自定义分页:分页样式采用bootstrap 2 class PageNation: 3 def __init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5): 4 ''' 5 :param base_url: 分页展示信息的基础路径 6 :param current_page_num: 当前页页码 7 :param total_counts: 总的数据量 8 :param per_page_counts: 每页展示的数据量 9 :param page_number: 显示页码数 10 ''' 11 self.base_url = base_url 12 self.current_page_num = current_page_num 13 self.total_counts = total_counts 14 self.per_page_counts = per_page_counts 15 self.page_number = page_number 16 self.request=request 17 import copy 18 self.params = copy.deepcopy(self.request.GET) 19 try: 20 self.current_page_num = int(self.current_page_num) 21 except Exception: 22 self.current_page_num = 1 23 half_page_range = self.page_number // 2 24 # 计算总页数 25 # self.page_number_count, a = divmod(self.total_counts, self.per_page_counts) 26 # if self.current_page_num < 1: 27 # self.current_page_num = 1 28 # if a: 29 # self.page_number_count += 1 30 import math 31 self.page_number_count=math.ceil(self.total_counts/self.per_page_counts) 32 if self.current_page_num < 1: 33 self.current_page_num = 1 34 if self.current_page_num > self.page_number_count: 35 self.current_page_num = self.page_number_count 36 if self.page_number_count <= self.page_number: 37 self.page_start = 1 38 self.page_end = self.page_number_count 39 else: 40 if self.current_page_num <= half_page_range: 41 self.page_start = 1 42 self.page_end = page_number 43 elif self.current_page_num + half_page_range >= self.page_number_count: 44 self.page_start = self.page_number_count - self.page_number + 1 45 self.page_end = self.page_number_count 46 else: 47 self.page_start = self.current_page_num - half_page_range 48 self.page_end = self.current_page_num + half_page_range 49 #数据切片依据,起始位置 50 @property 51 def start_num(self): 52 if self.current_page_num==0: 53 self.current_page_num=1 54 start_num = (self.current_page_num - 1) * self.per_page_counts 55 return start_num 56 #数据切片依据,终止位置 57 @property 58 def end_num(self): 59 end_num = self.current_page_num * self.per_page_counts 60 return end_num 61 # 拼接HTMl标签 62 def page_html(self): 63 tab_html = '' 64 tab_html += '<nav aria-label="Page navigation" class="pull-right"><ul class="pagination">' 65 # 页码 66 tab_html += f'<li><span aria-hidden="true" diabled>{self.current_page_num}/{self.page_number_count}</span></li>' 67 #首页 68 self.params['page'] = 1 69 first_page='<li><a href="{0}?{1}" ><span aria-hidden="true">首页</span></a></li>'.format(self.base_url, self.params.urlencode()) 70 tab_html+=first_page 71 # 上一页 72 if self.current_page_num == 1: 73 previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>' 74 else: 75 self.params['page'] = self.current_page_num - 1 76 previous_page = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>'.format( 77 self.base_url, self.params.urlencode()) 78 tab_html += previous_page 79 #数字页码 80 for i in range(self.page_start, self.page_end + 1): 81 # if self.current_page_num == i: 82 # one_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i) 83 # else: 84 # one_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(self.base_url, i) 85 # tab_html += one_tag 86 self.params['page'] = i 87 if self.current_page_num == i: 88 one_tag = '<li class="active"><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode()) 89 else: 90 one_tag = '<li><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode()) 91 tab_html += one_tag 92 # 下一页 93 if self.current_page_num == self.page_number_count: 94 next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>' 95 else: 96 self.params['page'] = self.current_page_num + 1 97 next_page = '<li><a href="{0}?{1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format(self.base_url, self.params.urlencode()) 98 tab_html += next_page 99 # 最后页 100 self.params['page'] = self.page_number_count 101 first_page = '<li><a href="{0}?{1}" ><span aria-hidden="true">尾页</span></a></li>'.format(self.base_url,self.params.urlencode()) 102 tab_html += first_page 103 # 总数据 104 tab_html += f'<li><span aria-hidden="true">共{self.total_counts}条</span></li>' 105 tab_html += '</ul></nav>' 106 return tab_html
函数低级版:
1 #函数低级版 2 def pagenation(base_url,current_page_num,total_counts,per_page_counts=10,page_number=5): 3 ''' 4 total_counts数据总数 5 per_page_counts每页分多少条数据 6 page_number = 页码显示多少个 7 current_page_num 当前页 8 :return: 9 ''' 10 # all_objs_list = models.Customer.objects.all() 11 # total_counts = all_objs_list.count() 12 # page_number = 5 13 try: 14 current_page_num = int(current_page_num) 15 except Exception: 16 current_page_num = 1 17 half_page_range = page_number//2 18 #计算总页数 19 page_number_count,a = divmod(total_counts,per_page_counts) 20 if current_page_num < 1: 21 current_page_num = 1 22 if a: 23 page_number_count += 1 24 if current_page_num > page_number_count: 25 current_page_num = page_number_count 26 start_num = (current_page_num - 1) * 10 27 end_num = current_page_num * 10 28 if page_number_count <= page_number: 29 page_start = 1 30 page_end = page_number_count 31 else: 32 if current_page_num <= half_page_range: 33 page_start = 1 34 page_end = page_number 35 elif current_page_num + half_page_range >= page_number_count: 36 page_start = page_number_count - page_number + 1 37 page_end = page_number_count 38 else: 39 page_start = current_page_num - half_page_range 40 page_end = current_page_num + half_page_range 41 #拼接HTMl标签 42 tab_html = '' 43 tab_html += '<nav aria-label="Page navigation"><ul class="pagination">' 44 #上一页 45 if current_page_num == 1: 46 previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>' 47 else: 48 previous_page = '<li><a href="{0}?page={1}" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>'.format(base_url,current_page_num-1) 49 tab_html += previous_page 50 for i in range(page_start,page_end+1): 51 if current_page_num == i: 52 one_tag = '<li class="active"><a href="{0}?page={1}">{1}</a></li>'.format(base_url,i) 53 else: 54 one_tag = '<li><a href="{0}?page={1}">{1}</a></li>'.format(base_url, i) 55 tab_html += one_tag 56 57 #下一页 58 if current_page_num == page_number_count: 59 next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>' 60 else: 61 next_page = '<li><a href="{0}?page={1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format(base_url,current_page_num+1) 62 tab_html+=next_page 63 tab_html += '</ul></nav>' 64 return tab_html,start_num,end_num