分页插件
分页的组件
这是一个基于类的组件,使用只需要实例化对象即可
参数:
:param current_page: 当前页码
:param all_count: 总数据条数
:param per_num: 每页显示数据条数
:param total_page: 总页码数
:param max_show: 最多显示页码数
:param page_start: 起始页码数
:param page_end: 终止页码数
:param start: 数据切片起始索引
:param end: 数据切片终止索引
代码:
""" 分页组件 """ from django.utils.html import mark_safe class Pagination(object): def __init__(self, request, all_count, base_url, per_num=10, max_show=11): try: current_page = int(request.GET.get('page')) if current_page <= 0: raise Exception() except Exception as e: current_page = 1 self.base_url = base_url self.current_page = current_page self.max_show = max_show self.half_show = max_show // 2 self.all_count = all_count self.per_num = per_num self.total_page, more = divmod(self.all_count, self.per_num) if more: self.total_page += 1 @property def start(self): return (self.current_page - 1) * self.per_num @property def end(self): return self.current_page * self.per_num @property def html_str(self): # 计算起始页码数和终止页码数 # 总页码数小于最大显示的页码数 if self.total_page < self.max_show: page_start = 1 page_end = self.total_page else: if self.current_page <= self.half_show: # 总页码数大于最大显示页码数 page_start = 1 page_end = self.max_show elif self.current_page + self.half_show > self.total_page: page_start = self.total_page - self.max_show + 1 page_end = self.total_page else: page_start = self.current_page - self.half_show page_end = self.current_page + self.half_show html_list = [] if self.current_page <= 1: prev_li = '<li class="disabled"><a><上一页></a></li>' else: prev_li = '<li><a href="{0}?page={1}"><上一页></a></li>'.format(self.base_url, self.current_page - 1) html_list.append(prev_li) for i in range(page_start, page_end + 1): if i == self.current_page: li_html = '<li class="active"><a href="{1}?page={0}">{0}</a></li>'.format(i, self.base_url) else: li_html = '<li><a href="{1}?page={0}">{0}</a></li>'.format(i, self.base_url) html_list.append(li_html) if self.current_page >= self.total_page: next_li = '<li class="disabled"><a>< 下一页 ></a></li>' else: next_li = '<li><a href="{1}?page={0}">< 下一页 ></a></li>'.format(self.current_page + 1, self.base_url) html_list.append(next_li) return mark_safe("".join(html_list))
带搜索的分页
此分页组件解决的页面有搜索时候分页跳转全部的问题
参数:
:param current_page: 当前页码
:param all_count: 总数据条数
:param per_num: 每页显示数据条数
:param total_page: 总页码数
:param max_show: 最多显示页码数
:param page_start: 起始页码数
:param page_end: 终止页码数
:param start: 数据切片起始索引
:param end: 数据切片终止索引
:param query_params: 搜索条件
代码展示:
class Pagination(object): def __init__(self, request, all_count, base_url, query_params, per_num=10, max_show=11): try: current_page = int(request.GET.get('page')) if current_page <= 0: raise Exception() except Exception as e: current_page = 1 self.base_url = base_url self.current_page = current_page self.query_params = query_params self.max_show = max_show self.half_show = max_show // 2 self.all_count = all_count self.per_num = per_num self.total_page, more = divmod(self.all_count, self.per_num) if more: self.total_page += 1 @property def start(self): return (self.current_page - 1) * self.per_num @property def end(self): return self.current_page * self.per_num @property def html_str(self): # 计算起始页码数和终止页码数 # 总页码数小于最大显示的页码数 if self.total_page < self.max_show: page_start = 1 page_end = self.total_page else: if self.current_page <= self.half_show: # 总页码数大于最大显示页码数 page_start = 1 page_end = self.max_show elif self.current_page + self.half_show > self.total_page: page_start = self.total_page - self.max_show + 1 page_end = self.total_page else: page_start = self.current_page - self.half_show page_end = self.current_page + self.half_show html_list = [] if self.current_page <= 1: prev_li = '<li class="disabled"><a><上一页></a></li>' else: self.query_params['page'] = self.current_page - 1 prev_li = '<li><a href="{0}?{1}"><上一页></a></li>'.format(self.base_url, self.query_params.urlencode()) html_list.append(prev_li) for i in range(page_start, page_end + 1): self.query_params['page'] = i if i == self.current_page: li_html = '<li class="active"><a href="{0}?{1}">{2}</a></li>'.format(self.base_url, self.query_params.urlencode(), i) else: li_html = '<li><a href="{0}?{1}">{2}</a></li>'.format(self.base_url, self.query_params.urlencode(), i) html_list.append(li_html) if self.current_page >= self.total_page: next_li = '<li class="disabled"><a>< 下一页 ></a></li>' else: self.query_params['page'] = self.current_page + 1 next_li = '<li><a href="{0}?{1}">< 下一页 ></a></li>'.format(self.base_url, self.query_params.urlencode()) html_list.append(next_li) return mark_safe("".join(html_list))
搜索字段:
方便搜索栏的字段限制
def get_search_conditon(self, request, fields_list): query = request.GET.get('query', '') print(query) q = Q() q.connector = 'OR' for i in fields_list: q.children.append(Q((f'{i}__contains', query))) return q
使用方法:
q = self.get_search_conditon(request, ['qq', 'qq_name'])
q是实力化限制字段的条件或条件,配合models查询条件使用
customer_obj = models.Customer.objects.filter(q, consultant__isnull=True)
query_params = copy.deepcopy(request.GET) query_params._mutable = True
获取搜索的搜索的需要的条件路径 pagin_obj = Pagination(request, len(customer_obj), request.path_info, query_params)
搜索的HTML:
<form action="" class="form-inline " method="get" > <div class="form-group "> <input type="text" class="form-control" name="query"> <button class="btn btn-primary btn-sm">搜索 <i class="fa fa-search"></i></button> </div> </form>