实现分页代码的封装,方便以后其他代码的调用
在project/app01/目录新建utils文件夹
文件夹下新建pagination.py文件
pagination.py
""" 自定义分页组件 以后如果想要使用这个分页组件,需要做: 在视图函数中: from app01.utils.pagination import Pagination def prettynum_list(request): #1.根据自己的情况去筛选自己的数据 queryset = models.PrettyNum.objects.all() #2.实例化分页对象 page_object = Pagination(request, queryset) context = { "search_data": search_data, 'queryset': page_object.page_queryset, #分完页的数据 "page_string": page_object.html() #生成的页码 } #返回到前端页面 return render(request, 'prettynum_list.html', context) 在HTML页面中: {% for obj in queryset %} <tr> <th>{{ obj.id }}</th> <td>{{ obj.mobile }}</td> <td>{{ obj.price }}</td> <td>{{ obj.get_level_display }}</td> <td>{{ obj.get_status_display }}</td> <td> <a class="btn btn-primary btn-xs" href="/prettynum/{{ obj.id }}/edit/">编辑</a> <a class="btn btn-danger btn-xs" href="/prettynum/{{ obj.id }}/delete/">删除</a> </td> </tr> {% endfor %} <ul class="pagination" > {{ page_string }} </ul> """ from django.utils.safestring import mark_safe class Pagination(object): def __init__(self, request,queryset,page_size = 10, page_param="page", plus = 5): """ :param request:请求的对象 :param queryset:符合条件的数据(根据此数据进行分页处理) :param page_size:每页显示多少条数据 :param page_param:获取在URL中传递的分页参数, 例如: /pretty/list/?page=21 :param plus:页码显示当前页的前几页后几页 """ page = int(request.GET.get(page_param, 1)) # 如果不是整数 if type(page) != int: # 强制让页码为1 page = 1 # if page.isdecimal(): # page = int(page) # else: # page = 1 self.page = page self.page_size = page_size self.start = (page - 1) * page_size self.end = page * page_size self.page_queryset = queryset[self.start:self.end] # 获取到符合条件的数据的总条数 total_count = queryset.count() total_page_count, div = divmod(total_count, page_size) if div: total_page_count += 1 self.total_page_count = total_page_count self.plus = plus def html(self): # 计算出显示当前页的前5页、后5页 if self.total_page_count <= 2 * self.plus + 1: # 数据库中的数据比较少,都没有达到11项 start_page = 1 end_page = self.total_page_count else: # 数据库中的数据比较多 大于11项 # 当前页小于5时, (小极值) if self.page <= self.plus: start_page = 1 end_page = 2 * self.plus + 1 else: # 当前页>5 # 当前页+5>总页面 if (self.page + self.plus) > self.total_page_count: start_page = self.total_page_count - 2 * self.plus end_page = self.total_page_count else: start_page = self.page - self.plus end_page = self.page + self.plus # 页码 page_str_list = [] # 首页 page_str_list.append('<li><a href="/prettynum/list/?page={}">首页</a></li>'.format(1)) # 上一页 if self.page > 1: prev = '<li><a href="/prettynum/list/?page={}">上一页</a></li>'.format(self.page - 1) else: prev = '<li><a href="/prettynum/list/?page={}">上一页</a></li>'.format(1) page_str_list.append(prev) # 页面 # range前取后不取,所以total_page_count+1 for i in range(start_page, end_page + 1): # 给当前页加一个样式 if i == self.page: ele = '<li class="active"><a href="/prettynum/list/?page={}">{}</a></li>'.format(i, i) else: ele = '<li><a href="/prettynum/list/?page={}">{}</a></li>'.format(i, i) page_str_list.append(ele) # 下一页 if self.page < self.total_page_count: prev = '<li><a href="/prettynum/list/?page={}">下一页</a></li>'.format(self.page + 1) else: prev = '<li><a href="/prettynum/list/?page={}">下一页</a></li>'.format(self.total_page_count) page_str_list.append(prev) # 尾页 page_str_list.append('<li><a href="/prettynum/list/?page={}">尾页</a></li>'.format(self.total_page_count)) search_string = """ <li> <form style="float: left; margin-left: -1px" method="get"> <input type="text" name="page" style="position: relative; float: left; display: inline-block; width: 80px; border-radius: 0;" class="form-control" placeholder="页码"> <button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button> </form> </li> """ page_str_list.append(search_string) page_string = mark_safe("".join(page_str_list)) return page_string
views.py
#实例化一个pagination from app01.utils.pagination import Pagination def prettynum_list(request): """靓号列表""" data_dict = {} search_data = request.GET.get('q', "") if search_data: data_dict[ "mobile__contains"] = search_data queryset = models.PrettyNum.objects.filter(**data_dict).order_by("-level") page_object = Pagination(request, queryset) context = { "search_data": search_data, 'queryset': page_object.page_queryset, #分完页的数据 "page_string": page_object.html() #生成的页码 } return render(request, 'prettynum_list.html', context)
解决小BUG
在搜索 + 分页情况下
当我们进行搜索时,可以展示出搜索的对应数据
但是如果此时我们进行翻页,搜索条件将清空,重新展示所有的数据
这样是不符合逻辑的分页时,应保留原来的搜索条件
http://127.0.0.1:8000/prettynum/list/?q=88 #搜索http://127.0.0.1:8000/prettynum/list/?page=30 #分页http://127.0.0.1:8000/prettynum/list/?q=88&page=30 #搜索+分页
pagination.py
""" 自定义分页组件 以后如果想要使用这个分页组件,需要做: 在视图函数中: from app01.utils.pagination import Pagination def prettynum_list(request): #1.根据自己的情况去筛选自己的数据 queryset = models.PrettyNum.objects.all() #2.实例化分页对象 page_object = Pagination(request, queryset) context = { "search_data": search_data, 'queryset': page_object.page_queryset, #分完页的数据 "page_string": page_object.html() #生成的页码 } #返回到前端页面 return render(request, 'prettynum_list.html', context) 在HTML页面中: {% for obj in queryset %} <tr> <th>{{ obj.id }}</th> <td>{{ obj.mobile }}</td> <td>{{ obj.price }}</td> <td>{{ obj.get_level_display }}</td> <td>{{ obj.get_status_display }}</td> <td> <a class="btn btn-primary btn-xs" href="/prettynum/{{ obj.id }}/edit/">编辑</a> <a class="btn btn-danger btn-xs" href="/prettynum/{{ obj.id }}/delete/">删除</a> </td> </tr> {% endfor %} <ul class="pagination" > {{ page_string }} </ul> """ from django.utils.safestring import mark_safe class Pagination(object): def __init__(self, request,queryset,page_size = 10, page_param="page", plus = 5): """ :param request:请求的对象 :param queryset:符合条件的数据(根据此数据进行分页处理) :param page_size:每页显示多少条数据 :param page_param:获取在URL中传递的分页参数, 例如: /pretty/list/?page=21 :param plus:页码显示当前页的前几页后几页 """ import copy query_dict = copy.deepcopy(request.GET) query_dict._mutable = True self.query_dict = query_dict self.page_param = page_param page = request.GET.get(page_param, 1) # 如果不是整数 if type(page) != int: # 强制让页码为1 page = 1 # if page.isdecimal(): # page = int(page) # else: # page = 1 self.page = page self.page_size = page_size self.start = (page - 1) * page_size self.end = page * page_size self.page_queryset = queryset[self.start:self.end] # 获取到符合条件的数据的总条数 total_count = queryset.count() total_page_count, div = divmod(total_count, page_size) if div: total_page_count += 1 self.total_page_count = total_page_count self.plus = plus def html(self): # 计算出显示当前页的前5页、后5页 if self.total_page_count <= 2 * self.plus + 1: # 数据库中的数据比较少,都没有达到11项 start_page = 1 end_page = self.total_page_count else: # 数据库中的数据比较多 大于11项 # 当前页小于5时, (小极值) if self.page <= self.plus: start_page = 1 end_page = 2 * self.plus + 1 else: # 当前页>5 # 当前页+5>总页面 if (self.page + self.plus) > self.total_page_count: start_page = self.total_page_count - 2 * self.plus end_page = self.total_page_count else: start_page = self.page - self.plus end_page = self.page + self.plus # 页码 page_str_list = [] self.query_dict.setlist(self.page_param, [1]) # 首页 page_str_list.append('<li><a href="?{}">首页</a></li>'.format(self.query_dict.urlencode())) # 上一页 if self.page > 1: self.query_dict.setlist(self.page_param, [self.page - 1]) prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode()) else: self.query_dict.setlist(self.page_param, [1]) prev = '<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode()) page_str_list.append(prev) # 页面 # range前取后不取,所以total_page_count+1 for i in range(start_page, end_page + 1): self.query_dict.setlist(self.page_param, [i]) # 给当前页加一个样式 if i == self.page: ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i) else: ele = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i) page_str_list.append(ele) # 下一页 if self.page < self.total_page_count: self.query_dict.setlist(self.page_param, [self.page + 1]) prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode()) else: self.query_dict.setlist(self.page_param, [self.total_page_count]) prev = '<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode()) page_str_list.append(prev) # 尾页 self.query_dict.setlist(self.page_param, [self.total_page_count]) page_str_list.append('<li><a href="?{}">尾页</a></li>'.format(self.query_dict.urlencode())) search_string = """ <li> <form style="float: left; margin-left: -1px" method="get"> <input type="text" name="page" style="position: relative; float: left; display: inline-block; width: 80px; border-radius: 0;" class="form-control" placeholder="页码"> <button style="border-radius: 0" class="btn btn-default" type="submit">跳转</button> </form> </li> """ page_str_list.append(search_string) page_string = mark_safe("".join(page_str_list)) return page_string
浏览器访问测试
标签:count,封装,分页,self,list,param,total,page From: https://www.cnblogs.com/kekeeleven/p/17771778.html