pagination(可以直接拿来用,基于bootstrap) 本文共有5490个字,关键词: 用法 ``` ###在视图函数中, def samplerlist(request): search_dict = {} search_key = request.GET.get('q', '') if search_key: search_dict["key"] = search_key data = models.xxx.objects.filter(**search_dict).order_by('-id') page_object = Pagination(request, data, ) context = { 'data': page_object.queryset, 'search_key': search_key, 'page_string': page_object.html() } ###在 模板页面中 {{ page_string }} ``` ``` from django.utils.safestring import mark_safe import copy class Pagination(object): def __init__(self, request, queryset, page_param='page', page_size=10, page_plus=5, show_prev_next=True, show_first_end=True): """ :param request:页面request :param queryset: 未分页前的dataset :param page_param: 分页参数名 :param page_size:分页大小 :param page_plus: 显示当前页的前后几页 :param show_prev_next:是否显示上一页和下一页 :param show_first_end:是否显示首页和尾页 """ query_dict=copy.deepcopy(request.GET) query_dict._mutable=True self.query_dict=query_dict page = request.GET.get(page_param, '1') if page.isdecimal(): page = int(page) else: page = 1 self.page = page self.page_size = page_size self.page_param=page_param self.page_plus = page_plus self.start = (page - 1) * page_size self.end = page * page_size self.show_prev_next = show_prev_next self.show_first_end = show_first_end self.queryset = queryset[self.start: self.end] self.total_count = queryset.count() total_page_count, div = divmod(self.total_count, self.page_size) if div: total_page_count += 1 self.total_page_count = total_page_count def html(self): if self.total_page_count <= 2 * self.page_plus + 1: """数据小于11页""" start_page = 1 end_page = self.total_page_count else: """数据大于等于11页""" if self.page <= self.page_plus: """当前页小于plus时""" start_page = 1 end_page = 2 * self.page_plus + 1 else: # 当前页大于plus # 如果当前前码加上plus超出了所有页码 if (self.page + self.page_plus) > self.total_page_count: start_page = self.total_page_count - 2 * self.page_plus end_page = self.total_page_count else: start_page = self.page - self.page_plus end_page = self.page + self.page_plus page_str_list = [''] if self.page > 1: if self.show_first_end: self.query_dict.setlist(self.page_param, [1]) page_str_list.append('首页'.format(self.query_dict.urlencode())) if self.show_prev_next: self.query_dict.setlist(self.page_param, [self.page - 1]) page_str_list.append('上一页'.format(self.query_dict.urlencode())) else: if self.show_first_end: page_str_list.append('首页') if self.show_prev_next: page_str_list.append('上一页') for i in range(start_page, end_page + 1): if i == self.page: # 当前页 ele = '{}'.format(i) else: self.query_dict.setlist(self.page_param, [i]) ele = '{}'.format(self.query_dict.urlencode(), i) page_str_list.append(ele) if self.page < self.total_page_count: if self.show_prev_next: self.query_dict.setlist(self.page_param, [self.page + 1]) page_str_list.append('下一页'.format(self.query_dict.urlencode())) if self.show_first_end: self.query_dict.setlist(self.page_param, [self.total_page_count]) page_str_list.append('尾页'.format(self.query_dict.urlencode())) else: if self.show_prev_next: page_str_list.append('下一页') if self.show_first_end: page_str_list.append('尾页') # 跳转 # search_string = """ # # # # 跳转 # # # """ # page_str_list.append(search_string) page_str_list.append('') page_string = "".join(page_str_list) return mark_safe(page_string) ``` [Pagination.py][1] [1]: /usr/uploads/2022/04/2762412001.py 「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」 赞赏 × 梦白沙 (๑>ڡ<)☆谢谢老板~ 1元 2元 5元 10元 50元 任意金额 2元 使用微信扫描二维码完成支付 版权声明:本文为作者原创,如需转载须联系作者本人同意,未经作者本人同意不得擅自转载。 django 2022-04-11 评论 391 次浏览