封装的分页功能
page.py
# !/usr/bin/env python # -*- coding: utf-8 -*- # Time : 2020/6/7 18:38 # Author : Eunice import re from django.utils.safestring import mark_safe class MyPagenation(object): def __init__(self, page_num, total_count, base_url, per_page_num=10, page_num_show=5, get_data=None): """ :param page_num: 当前页码数 :param total_count: 总的数据量 :param per_page_num: 每页显示的行数 :param get_data: 搜索时的参数 """ self.get_data = get_data # 搜索条件 self.per_page_num = per_page_num # 每页显示10行数据 self.page_num_show = page_num_show # 页面生成分页页码的数量 self.base_url = base_url quotient, remainder = divmod(total_count, self.per_page_num) # quotient商, remainder余数 # 总页码数 if remainder: page_num_count = quotient + 1 # 有余数 页数+1 else: page_num_count = quotient self.page_num_count = page_num_count # 兼容当前页码数 try: page_num = int(page_num) except Exception: page_num = 1 if page_num < 0: page_num = 1 elif page_num > page_num_count: # 兼容输入大于总页码的值 page_num = page_num_count # 返回最后一页 half_num = self.page_num_show // 2 """ 3 4 5 6 7 5 6 7 8 9 """ if page_num - half_num <= 0: # 页码为负数时 start_num = 1 end_num = self.page_num_show # 显示分页的 elif page_num + half_num > page_num_count: # 页码大于总的页码数 start_num = page_num_count - self.page_num_show + 1 end_num = page_num_count else: start_num = page_num - half_num # 开始页码 end_num = page_num + half_num # 结束页码 if page_num_count < page_num_show: # 总的页码数小于展示的页码数 start_num = 1 end_num = page_num_count self.page_num = page_num self.start_num = start_num self.end_num = end_num @property def cal_start_data(self): return (self.page_num - 1) * self.per_page_num @property def cal_end_data(self): return self.page_num * self.per_page_num @property def html_page(self): page_num_range = range(self.start_num, self.end_num + 1) page_html = '' page_pre_html = '<nav aria-label="Page navigation"><ul class="pagination">' page_html += page_pre_html first_page_html = '<li><a href="{1}?page={0}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format( 1, self.base_url) # 首页标签 page_html += first_page_html if self.page_num == 1: pre_page = '<li class="disabled"><a href="javascript: void(0)" aria-label="Previous"><span aria-hidden="true">«</span></a></li>' # 如果当前页在第一页上,上一页的标签不能点击 else: pre_page = '<li><a href="{1}?page={2}{0}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(self.page_num - 1, self.base_url, re.sub('page=d+', '', self.get_data) if 'page=' in self.get_data else self.get_data + '&') # 上一页的标签,三元运算符判断是否含有page字段 page_html += pre_page for i in page_num_range: if self.page_num == i: page_html += '<li class="active"><a href="{1}?{2}page={0}">{0}</a></li>'.format(i, self.base_url, re.sub('page=d+', '', self.get_data) if 'page=' in self.get_data else self.get_data + '&') else: page_html += '<li><a href="{1}?{2}page={0}">{0}</a></li>'.format(i, self.base_url, re.sub('page=d+', '', self.get_data) if 'page=' in self.get_data else self.get_data + '&') if self.page_num == self.page_num_count: nex_page = '<li class="disabled"><a href="javascript: void(0)" aria-label="Next"><span aria-hidden="true">»</span></a></li>' # 下一页的标签 else: nex_page = '<li><a href="{1}?{2}page={0}" aria-label="Next"><span aria-hidden="true">»</span></a></li>'.format( self.page_num + 1, self.base_url, re.sub('page=d+', '', self.get_data) if 'page=' in self.get_data else self.get_data + '&') # 下一页的标签 page_html += nex_page page_next_html = '</ul></nav>' last_page_html = '<li><a href="{1}?{2}page={0}" aria-label="Previous"><span aria-hidden="true">最后一页</span></a></li>'.format( self.page_num_count, self.base_url, re.sub('page=d+', '', self.get_data) if 'page=' in self.get_data else self.get_data + '&') # 尾页标签 page_html += last_page_html page_html += page_next_html return mark_safe(page_html)
views.py
def customer(request): get_data = request.GET.urlencode() # search_field=name__contains&kw=%E4%B8%9C search_field = request.GET.get('search_field') # 选择搜索字段 kw = request.GET.get('kw') # 搜索关键字 if kw: # customer_list = models.Customer.objects.filter(**{search_field: kw}) # 另一种实现方式,支持或查询 q_obj = Q() # q条件查询的连接符默认是&连接 q_obj.children.append((search_field, kw)) # 可以拿到一组关键字 Q(qq=kw) # q_obj.children.append((search_field2, kw)) # 添加另一组关键字 Q(name=kw)相当于Q(qq=kw)&Q(name=kw) # q_obj.connector = 'or' # 修改连接符为or Q(qq=kw)|Q(name=kw) customer_list = models.Customer.objects.filter(q_obj) else: customer_list = models.Customer.objects.all() page_num = request.GET.get('page') # 获取当前页码数 customer_count = customer_list.count() per_page_num = settings.PER_PAGE_NUM page_num_show = settings.PAGE_NUM_SHOW base_url = request.path # 访问的路径 page_obj = mypage(page_num, customer_count, base_url, per_page_num, page_num_show, get_data) customer_obj = customer_list.reverse()[page_obj.cal_start_data: page_obj.cal_end_data] # return render(request, 'saleshtml/customers.html', {'customer_obj': customer_obj, 'page_num_count': page_num_range}) return render(request, 'saleshtml/customers.html', {'customer_obj': customer_obj, 'page_html': page_obj.html_page})