博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Django自定义分页并保存搜索条件
阅读量:5350 次
发布时间:2019-06-15

本文共 4493 字,大约阅读时间需要 14 分钟。

Django自定义分页并保存搜索条件

1、自定义分页组件pagination.py

import copyclass Pagination:    def __init__(self, current_page_num, all_count, request, per_page_num=10, page_count=11):        """        封装分页相关数据        :param current_page_num: 当前页码数        :param all_count: 数据库中总数据条数        :param per_page_num: 每页显示的数据量        :param page_count: 最大显示页数        """        try:            current_page = int(current_page_num)        except Exception as e:            current_page = 1        # 页码数小于1时显示第一页        if current_page < 1:            current_page = 1        # 取总页码数        self.all_page_count, temp = divmod(all_count, per_page_num)        if temp:            self.all_page_count += 1        # 页码数大于最大页码数时显示最后一页        if current_page > self.all_page_count:            current_page = self.all_page_count        self.current_page = current_page        self.per_page_num = per_page_num        self.half_page_count = int((page_count - 1) / 2)        # 总页码数小于最大页码数        if self.all_page_count < page_count:            self.start_page = 1            self.end_page = self.all_page_count + 1        else:            if self.current_page < self.half_page_count:                # 当页码条数据靠近最左边时                self.start_page = 1                self.end_page = page_count + 1            elif self.all_page_count - self.current_page < self.half_page_count:                # 当页码条数据靠近最右边时                self.start_page = self.all_page_count - page_count + 1                self.end_page = self.all_page_count + 1            else:                # 页码条正常显示在中间位置                self.start_page = self.current_page - self.half_page_count                self.end_page = self.current_page + self.half_page_count + 1        # 获取get参数: 
params = request.GET # 不进行deepcopy将无法改变其里面的值 self.new_params = copy.deepcopy(params) @property def start(self): return (int(self.current_page - 1)) * self.per_page_num @property def end(self): return int(self.current_page) * self.per_page_num def show_html(self): html_list = [] if self.current_page == 1: first_page = '
  • {0}
  • '.format('首页') else: # 将page传入new-params中 self.new_params['page'] = 1 # 对new_params进行urlencode格式化: 'key=value&key2=value2&page=1' first_page = '
  • {1}
  • '.format(self.new_params.urlencode(), '首页') # 将"首页"html代码加入html_list中 html_list.append(first_page) if self.current_page == 1: prev_page = '
  • {0}
  • '.format('«') else: self.new_params['page'] = self.current_page - 1 prev_page = '
  • {1}
  • '.format(self.new_params.urlencode(), '«') # 将"上一页"html代码加入html_list中 html_list.append(prev_page) for i in range(self.start_page, self.end_page): if self.current_page == i: page_bar = '
  • {0}
  • '.format(i) else: self.new_params['page'] = i page_bar = '
  • {1}
  • '.format(self.new_params.urlencode(), i) # 将"每一页"html代码加入html_list中 html_list.append(page_bar) if self.current_page == self.all_page_count: next_page = '
  • {0}
  • '.format('»') else: self.new_params['page'] = self.current_page + 1 next_page = '
  • {1}
  • '.format(self.new_params.urlencode(), '»') # 将"下一页"html代码加入html_list中 html_list.append(next_page) if self.current_page == self.all_page_count: last_page = '
  • {0}
  • '.format('尾页') else: self.new_params['page'] = self.all_page_count last_page = '
  • {1}
  • '.format(self.new_params.urlencode(), '尾页') # 将"尾页"html代码加入到html_list中 html_list.append(last_page) return ''.join(html_list)

    2、view视图

    class CustomerView(View):    def get(self, request):        customer_list = Customer.objects.all()        page = request.GET.get('page')        # 实例化pagination对象        pagination = Pagination(page, customer_list.count(), request, per_page_num=1)        # 对数据列表进行分页        customer_list = customer_list[pagination.start:pagination.end]        context = {            'customer_list': customer_list,            'page_html': pagination.show_html()        }        return render(request, 'customer_list.html', context)    def post(self, request):        pass

    3、templates模板

    4、页面展示

     

    转载于:https://www.cnblogs.com/wangyueping/p/11380793.html

    你可能感兴趣的文章
    idea2019中utf-8乱码问题
    查看>>
    docker应用-3(搭建hadoop以及hbase集群)
    查看>>
    生成keystory文件
    查看>>
    ubuntu 网络配置
    查看>>
    char能表示(-128~127)
    查看>>
    Linux 性能监测:Memory
    查看>>
    正则化(尚未完成)
    查看>>
    DrRacket
    查看>>
    编程之美之中国象棋将帅问题
    查看>>
    PHP发送HEADER头消息
    查看>>
    文件上传
    查看>>
    entity framework 连接 oracle 发布后出现的问题(Unable to find the requested .Net Framework Data Provider)...
    查看>>
    Python多线程Selenium跨浏览器测试
    查看>>
    日期-date.js
    查看>>
    JavaSE02:第一个程序
    查看>>
    linux下安装jdk8
    查看>>
    ubuntu gcc 降级 适应matlab
    查看>>
    [转]Three things you should never put in your database
    查看>>
    执行makemigrations后错误集锦
    查看>>
    JDBC高级部分
    查看>>