自定义分页:
功能:
- 首尾页跳转
- 保存搜索条件
#自定义分页 #官方推荐,页码数为奇数 class PageNation: def __init__(self,base_url,current_page_num,total_counts,request,per_page_counts=10,page_number=5,): ''' :param base_url: 分页展示信息的基础路径 :param current_page_num: 当前页页码 :param total_counts: 总的数据量 :param per_page_counts: 每页展示的数据量 :param page_number: 显示页码数 ''' self.base_url = base_url self.current_page_num = current_page_num self.total_counts = total_counts self.per_page_counts = per_page_counts self.page_number = page_number self.request = request try: self.current_page_num = int(self.current_page_num) except Exception: self.current_page_num = 1 if self.current_page_num < 1: self.current_page_num = 1 half_page_range = self.page_number // 2 # 计算总页数 self.page_number_count, a = divmod(self.total_counts, self.per_page_counts) if a: self.page_number_count += 1 if self.current_page_num > self.page_number_count: self.current_page_num = self.page_number_count if self.page_number_count <= self.page_number: self.page_start = 1 self.page_end = self.page_number_count else: if self.current_page_num <= half_page_range: #2 self.page_start = 1 self.page_end = page_number #5 elif self.current_page_num + half_page_range >= self.page_number_count: self.page_start = self.page_number_count - self.page_number + 1 self.page_end = self.page_number_count else: self.page_start = self.current_page_num - half_page_range self.page_end = self.current_page_num + half_page_range import copy from django.http.request import QueryDict self.params = copy.deepcopy(request.GET) # ?condition = qq & wd = 1 & page = 3 # params['page'] = current_page_num # query_str = params.urlencode() #数据切片依据,起始位置 @property def start_num(self): start_num = (self.current_page_num - 1) * self.per_page_counts return start_num #数据切片依据,终止位置 @property def end_num(self): end_num = self.current_page_num * self.per_page_counts return end_num # 拼接HTMl标签 def page_html(self): tab_html = '' tab_html += '<nav aria-label="Page navigation" class="pull-right"><ul class="pagination">' #首页 self.params['page'] = 1 showye = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">首页</span></a></li>'.format(self.base_url,self.params.urlencode()) tab_html += showye # 上一页 if self.current_page_num == 1: previous_page = '<li disabled><a href="#" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>' else: self.params['page'] = self.current_page_num - 1 previous_page = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">«</span></a></li>'.format( self.base_url,self.params.urlencode()) tab_html += previous_page #循环生成页码标签 for i in range(self.page_start, self.page_end + 1): # request.GET {condition: qq, wd: 1,'page':1} request.GET.urlencode() condition=qq&wd=1&page=4 self.params['page'] = i # {condition: qq, wd: 1,'page':1} urlencode() -- condition=qq&wd=1&page=4 if self.current_page_num == i: one_tag = '<li class="active"><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode()) #?condition=qq&wd=1&page=3 else: one_tag = '<li><a href="{0}?{2}">{1}</a></li>'.format(self.base_url, i,self.params.urlencode()) tab_html += one_tag # 下一页 if self.current_page_num == self.page_number_count: next_page = '<li disabled><a href="#" aria-label="Next"><span aria-hidden="true">»</span></a></li>' else: self.params['page'] = self.current_page_num + 1 next_page = '<li><a href="{0}?{1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format(self.base_url, self.params.urlencode()) tab_html += next_page # 尾页 self.params['page'] = self.page_number_count weiye = '<li><a href="{0}?{1}" aria-label="Previous" ><span aria-hidden="true">尾页</span></a></li>'.format( self.base_url, self.params.urlencode()) tab_html += weiye tab_html += '</ul></nav>' return tab_html
# 公共客户信息展示 @login_required def customers(request): # 获取用户选择的和用户输入的搜索内容,默认为空,不选择和不输入get的返回结果为None choice = request.GET.get("choice",'') wd = request.GET.get("wd",'') # 在查询时设置成包含的情况contains--->关键字 choice = choice + '__contains' # 默认get第一次请求第一页 current_page_num = request.GET.get('page', 1) if wd: # 实例化一个Q对象 q = Q() # 指定连接条件,默认是and # q.connector = "or" # 必须是元组,因为只接收一个参数 q.children.append((choice,wd)) # q.children.append(('name__contains', '小')) # 名字中包含wd字段的所有信息 qq__contains 66 name__contains 小 # models.Customer.objects.filter(name__contains=wd) customers_obj = models.Customer.objects.filter(q) else: # 所有销售为空的,就是公共客户 customers_obj = models.Customer.objects.filter(consultant__isnull=True) # 每页展示的数据量 page_counts = 5 # 页码数 page_number = 7 # 总数据 total_count = customers_obj.count() if total_count: # 实列化分页的类 page_obj = page.PageNation(request.path, current_page_num, total_count, request,page_counts, page_number) # 切片:从总数居中每次切出多少条数据展示在页面上 customers_obj = customers_obj.order_by('-pk')[page_obj.start_num:page_obj.end_num] ret_html = page_obj.page_html() return render(request,"customers.html",{"customers_obj":customers_obj,"ret_html":ret_html}) else: return render(request,"404.html")