博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
分页器(自定制)
阅读量:5902 次
发布时间:2019-06-19

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

一、函数版  (未更新)

1 def show_books(request): 2     """ 3     自定制分页器(函数版): 4     :param request:  5     :return:    data_list: 希望展示在当前页面的数据集合 6                 page_html: 页码导航条html 7     """ 8     url = request.path_info 9 10     all_book_list = models.Book.objects.all()11     total_data = all_book_list12 13     try:14         total_data_num = total_data.count()  # 总数据条数15     except TypeError:16         total_data_num = len(total_data)17 18     current_page_id = request.GET.get('page')  # 当前页码id19     try:20         current_page_id = int(current_page_id)21     except (ValueError, TypeError):22         current_page_id = 123 24     perpage_data_num = 10  # 每个页面展示数据条数25 26     quotient, more = divmod(total_data_num, perpage_data_num)27     total_page_num = quotient+1 if more != 0 or quotient == 0 and more == 0 else quotient  # 总页码数28 29     # 控制当前页码乱入负数、太大数等情况30     if current_page_id < 1:31         current_page_id = 132     elif current_page_id > total_page_num:33         current_page_id = total_page_num34 35     pagedata_start_id = (current_page_id - 1) * perpage_data_num36     pagedata_end_id = current_page_id * perpage_data_num37 38     nvgtnbar_page_num = 9  # 页面中页码导航条显示的页码个数39     left_page_id = current_page_id - nvgtnbar_page_num // 2  # 页码导航条最左边的页码id40     right_page_id = current_page_id + nvgtnbar_page_num // 2  # 页码导航条最右边的页码id41 42     # 两端控制溢出43     if left_page_id < 1:44         left_page_id = 145         right_page_id = nvgtnbar_page_num46     if right_page_id > total_page_num:47         right_page_id = total_page_num48         left_page_id = total_page_num - nvgtnbar_page_num + 149 50     # 数据量不足的情况下,控制页码导航条的显示51     if total_page_num < nvgtnbar_page_num:52         left_page_id = 153         right_page_id = total_page_num54 55     page_html = ''56     prev_page_id = current_page_id - 1 if current_page_id != 1 else current_page_id57     next_page_id = current_page_id + 1 if current_page_id != total_page_num else current_page_id58     first_page_html = '
  • 首页
  • '.format(url)59 last_page_html = '
  • 尾页
  • '.format(url, total_page_num)60 prev_page_html = '
  • 上一页
  • '.format(url, prev_page_id)61 next_page_html = '
  • 下一页
  • '.format(url, next_page_id)62 63 for i in range(left_page_id, right_page_id + 1):64 if i == current_page_id:65 page_html += '
  • {1}
  • '.format(url, i)66 else:67 page_html += '
  • {1}
  • '.format(url, i)68 69 page_html = prev_page_html + first_page_html + page_html + last_page_html + next_page_html70 71 data_list = total_data[pagedata_start_id:pagedata_end_id]72 73 return render(request, 'book_list.html', {
    'data_list': data_list, 'page_html': page_html})
    分页器示例函数(未封装)

    二、分页器(封装版)  (已更新)

    class Pagination(object):    def __init__(self, request, data_set, perpage_data_count=10, nvgbar_page_count=9):        """        自定制分页器:                        供调用属性                        设定接收分页器处理后结果的对象为mypage                        mypage.data:     希望展示在当前页面上的数据集合                        mypage.page_html:页码导航栏html        :param request:     request请求        :param data_set:        希望进行分页处理的容器性数据对象,即总数据        :param perpage_data_count:    希望每个页面要展示的数据条数,默认10条        :param nvgbar_page_count:    希望页码导航条展示的页码数,默认9个页码        """        self.url = request.path_info        self.perpage_data_count = perpage_data_count        self.nvgbar_page_count = nvgbar_page_count        current_page_id = request.GET.get('page')        try:            current_page_id = int(current_page_id)        except (ValueError, TypeError):            current_page_id = 1        self.current_page_id = current_page_id        try:            total_data_count = data_set.count()        except TypeError:            total_data_count = len(data_set)        # quotient, more = divmod(total_data, self.perpage_data)        # self.total_page_count = quotient + 1 if more != 0 or quotient == 0 and more == 0 else quotient  # 总页码数        self.total_page_count, more = divmod(total_data_count, self.perpage_data_count)        if more:            self.total_page_count += 1        # 控制当前页码乱入负数、太大数等情况(两个if,顺序不能反)        if self.current_page_id > self.total_page_count:            self.current_page_id = self.total_page_count        if self.current_page_id < 1:            self.current_page_id = 1        perpagedata_start = (self.current_page_id - 1) * self.perpage_data_count        perpagedata_end = self.current_page_id * self.perpage_data_count        self.data = data_set[perpagedata_start: perpagedata_end]    @property    def page_html(self):        left_page_id = self.current_page_id - self.nvgbar_page_count // 2  # 页码导航条最左边的页码id        right_page_id = self.current_page_id + self.nvgbar_page_count // 2  # 页码导航条最右边的页码id        # 两端控制溢出        if left_page_id < 1:            left_page_id = 1            right_page_id = self.nvgbar_page_count        if right_page_id > self.total_page_count:            right_page_id = self.total_page_count            left_page_id = self.total_page_count - self.nvgbar_page_count + 1        # 数据量不足的情况下,控制页码导航条的显示        if self.total_page_count < self.nvgbar_page_count:            left_page_id = 1            right_page_id = self.total_page_count        page_html_list = ['
    ') page_html = ''.join(page_html_list) return page_html
    分页器(封装版)
    视图中使用分页器:
    from utils.mypage import Pagination    def show_books(request):    book_list = models.Books.objects.all()    # book_list = models.Books.objects.all().value_list('title', 'authors')    show_obj = Pagination(request, book_list)    return render(request, 'books.html', {
    'show_obj': show_obj})

    模板中:

        
    Title
    {
    % for book in show_obj.data %}
    {
    % endfor %}
    序号 ID 书名
    {
    { forloop.counter }}
    {
    { book.id }}
    {
    { book.title }}
    {
    { show_obj.page_html|safe }}
    模板html中调用

     

     

     

     

    ok

     

    转载于:https://www.cnblogs.com/kingon/p/9452240.html

    你可能感兴趣的文章
    读书笔记之《实战Java虚拟机》(9):Class 文件结构
    查看>>
    1024城市峰会 | 当A.I.邂逅古都西安
    查看>>
    好看的卡片阴影
    查看>>
    理解 Mach O 并提高程序启动速度
    查看>>
    Vue实战篇(PC端商城项目)
    查看>>
    你要做的是产品经理,不是作图经理!
    查看>>
    JavaEE 项目常见错误汇总
    查看>>
    快速掌握Python基础语法(下)
    查看>>
    【Android自定义View】绘图之文字篇(三)
    查看>>
    适配iOS 11和iPhoneX屏幕适配遇到的一些坑
    查看>>
    Fetch API 简单封装
    查看>>
    给媳妇做一个记录心情的小程序
    查看>>
    iOS App无需跳转系统设置自动连接Wi-Fi
    查看>>
    一道柯里化面试题
    查看>>
    本科studying abroad 无法毕业申请硕士转学转校处理一切studying abroad 问题
    查看>>
    RxJava(RxAndroid)的简单学习
    查看>>
    Java8 函数式编程之函数接口(下)
    查看>>
    【本人秃顶程序员】MySQL 全表 COUNT(*) 简述
    查看>>
    centos7中使用febootstrap自制一个基础的centos 7.2的docker镜像
    查看>>
    C#开发Unity游戏教程之判断语句
    查看>>