你的客户数据非常非常多,分很多页,所以我这里把分页应用上,但如下图中我只有7个客户数据:
因为我的分页器pagination.py中是设置了每页面最多显示10条,所以上述客户数据不足以显示多页,因此我把设置成每页最多显示2条,这样就可多页了,
viwes.py中:
# 展示客户列表CBV
class CustomerList(View):
def get(self, request):
q = self.get_search_contion(['qq','name'])
if request.path_info == reverse('customer'):
all_customer = models.Customer.objects.filter(q,consultant__isnull=True)
else:
all_customer = models.Customer.objects.filter(q,consultant=request.user)
page = Pagination(request, all_customer.count(),2)#每页最多显示2个
.............
如下图:
但是这样就有一个问题了:如下图我搜索含5的客户:第一页它是显示出搜索结果的,但是第二页就显示page=2页面(事实上我含5的客户有4条数据即应该有2个页面的搜索结果显示),那我的搜索条件去哪了?--现在的情况是搜索结果后,我点下一个页码搜索结果就没了
只能通过手动地址栏输入?query=5&page=2才能拿到搜索结果的第二页数据(其实搜索结果的第一页url也是query=5&page=1),如下图:
所以应该在分页器对象里把搜索条件也定义上:
(1)先获取搜索条件在url中拿print(request.GET)拿到参数是
from django.http import QueryDict
(2)所以把request.GET传到分页中,且我要把字典样式做修改:query=5&page=2让它也支持这样的搜索条件用request.GET._mutable=True属性设置成支持修改
(3)应该对request.GET做一个深拷贝--以免万一以后你还用到request.GET这个参数,因为你修改后这个request.GET参数就变了
import copy
query_params=copy.deepcopy(request.GET)
(4)现在就可把这个字典参数传给分页器对象了:
page = Pagination(request, all_customer.count(),query_params,2)
(5)并且让你分页器类要定义接收此参数:pagination.py中:
class Pagination: #把分页封装成一个类
def __init__(self,request,all_count,query_params,per_num=10,max_show=11):
最后代码修改如下:
1.views.py中:
from django.http import QueryDict
import copy
# 展示客户列表CBV
class CustomerList(View):
def get(self, request):
q = self.get_search_contion(['qq','name'])
if request.path_info == reverse('customer'):
all_customer = models.Customer.objects.filter(q,consultant__isnull=True)
else:
all_customer = models.Customer.objects.filter(q,consultant=request.user)
query_params = copy.deepcopy(request.GET) #对字典深拷贝
page = Pagination(request, all_customer.count(),query_params,2)
return render(request, 'crm/customer_list.html',
{"all_customer": all_customer[page.start:page.end], 'pagination': page.show_li})
........省略.....
2.pagination.py中:
from django.utils.safestring import mark_safe from django.http import QueryDict class Pagination: #把分页封装成一个类 def __init__(self,request,all_count,query_params=QueryDict(),per_num=10,max_show=11):#把查询条件字典参数设成默认值并为空 # 基本的URL self.base_url = request.path_info #查询条件 self.query_params = query_params#给它赋值
self. query_params._mutable = True#_mutable属性是支持把这个字典做修改 # 当前页码 try: self.current_page = int(request.GET.get('page', 1)) if self.current_page <= 0: self.current_page = 1 except Exception as e: self.current_page = 1 # 最多显示的页码数 self.max_show = max_show half_show = max_show // 2 # 每页显示的数据条数 self.per_num = per_num # 总数据量 self.all_count = all_count # 总页码数 self.total_num, more = divmod(all_count, per_num) if more: self.total_num += 1 # 总页码数小于最大显示数:显示总页码数 if self.total_num <= max_show: self.page_start = 1 self.page_end = self.total_num else: # 总页码数大于最大显示数:最多显示11个 if self.current_page <= half_show: self.page_start = 1 self.page_end = max_show elif self.current_page + half_show >= self.total_num: self.page_end = self.total_num self.page_start = self.total_num - max_show + 1 else: self.page_start = self.current_page - half_show self.page_end = self.current_page + half_show @property #@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 show_li(self): # 存放li标签的列表 html_list = [] self.query_params['page'] = 1 # query=alex&page=1 first_li = '
3.这样就实现搜索结果能保留多页显示了!!!