• Django简单分页器


    views.py函数 :

    # 测试数据
    obj_data = [{"name": "wang{}".format(i), "pwd": "dong{}".format(i)} for i in range(1, 302)]
    def page(request):
        page = Pagination(request, len(obj_data))
        return render(request, "page.html", {
            "obj": obj_data[page.start(): page.end()],
            # "page_num": range(start_page, end_page +1)
            "html_str": page.show_li()
        })

    封装成函数版本 :

    """
    分页器
    """
    from django.utils.safestring import mark_safe
    
    
    class Pagination:
        def __init__(self, request, all_data, data_num=10, max_page=5):
            # 当前页面数, 异常处理防止越界操作(page等于字符串或负数)
            self.base_url = request.path_info
            try:
                self.now_page = int(request.GET.get("page", 1))
                if self.now_page <= 0:
                    self.now_page = 1
            except Exception as e:
                self.now_page = 1
            # 每页显示的数据量
            self.data_num = data_num
            # 总数据量
            self.all_data = all_data
            # 总页码数
            self.page_num, more = divmod(all_data, data_num)
            if more:
                self.page_num += 1
            # 单次最多显示的页码数
            self.max_page = max_page
            # 最大页码数的一半
            half_max_page = max_page // 2
            # 数据的总页码数不足设置的最大单次显示页码数
            if self.page_num <= max_page:
                self.start_page = 1
                self.end_page = self.page_num
            else:
                # 数据的总页码数大于设置的最大单次显示页码数
                if self.now_page <= half_max_page:
                    self.start_page = 1
                    self.end_page = max_page
                elif self.now_page + half_max_page >= self.page_num:
                    self.start_page = self.page_num - max_page + 1
                    self.end_page = self.page_num
                else:
                    self.start_page = self.now_page - half_max_page
                    self.end_page = self.now_page + half_max_page
    
        def start(self):
            return (self.now_page - 1) * self.data_num
    
        def end(self):
            return self.now_page * self.data_num
    
        def show_li(self):
            # 放html标签的列表
            html_list = []
            # 首页显示按钮
            first_li = "<li><a href='{}?page=1'>首页</a></li>".format(self.base_url)
            html_list.append(first_li)
            # 如果当前页码是第一页, 则上一页按键禁用
            if self.now_page == 1:
                before_li = "<li class='disabled'><a href='{1}?page={0}'><<</a></li>".format(self.now_page - 1, self.base_url)  # 上一页
            else:  # 当前页码不是第一页, 正常显示
                before_li = "<li><a href='{1}?page={0}'><<</a></li>".format(self.now_page - 1, self.base_url)  # 上一页
            html_list.append(before_li)
            # 对起始页码到终止页码for循环
            for num in range(self.start_page, self.end_page + 1):
                if num == self.now_page:
                    li_html = "<li class = 'active'><a href='{1}?page={0}'>{0}</a></li>".format(num, self.base_url)
                else:
                    li_html = "<li><a href='{1}?page={0}'>{0}</a></li>".format(num, self.base_url)
                html_list.append(li_html)
            # 如果当前页码是最后一页, 下一页按键禁用
            if self.now_page == self.page_num:
                next_li = "<li class='disabled'><a href='{1}?page={0}'>>></a></li>".format(self.now_page + 1, self.base_url)  # 下一页
            else:  # 非最后一页, 正常显示
                next_li = "<li><a href='{1}?page={0}'>>></a></li>".format(self.now_page + 1, self.base_url)  # 下一页
            html_list.append(next_li)
            # 加尾页显示按钮
            last_li = "<li><a href='{1}?page={0}'>尾页</a></li>".format(self.page_num, self.base_url)
            # 将定义好的标签放入列表
            html_list.append(last_li)
            # 将列表使用""组合成字符串
            return mark_safe("".join(html_list))

    直接写成函数版本 :

    def page(request):
        # # 当前页面数, 异常处理防止越界操作(page等于字符串或负数)
        # try:
        #     now_page = int(request.GET.get("page", 1))
        #     if now_page <= 0:
        #         now_page = 1
        # except Exception as e:
        #     now_page = 1
        # # 每页显示的数据量
        # data_num = 10
        # # 总数据量
        # all_data = len(obj_data)
        # # 总页码数
        # page_num, more = divmod(all_data, data_num)
        # if more:
        #     page_num += 1
        # # 单次最多显示的页码数
        # max_page = 5
        # # 最大页码数的一半
        # half_max_page = max_page // 2
        # # 数据的总页码数不足设置的最大单次显示页码数
        # if page_num <= max_page:
        #     start_page = 1
        #     end_page = page_num
        # else:
        #     # 数据的总页码数大于设置的最大单次显示页码数
        #     if now_page <= half_max_page:
        #         start_page = 1
        #         end_page = max_page
        #     elif now_page + half_max_page >= page_num:
        #         start_page = page_num - max_page +1
        #         end_page = page_num
        #     else:
        #         start_page = now_page - half_max_page
        #         end_page = now_page + half_max_page
        #
        # # 放html标签的列表
        # html_list = []
        # # 首页显示按钮
        # first_li = "<li><a href='/page/?page=1'>首页</a></li>"
        # html_list.append(first_li)
        # # 如果当前页码是第一页, 则上一页按键禁用
        # if now_page == 1:
        #     before_li = "<li class='disabled'><a href='/page/?page={0}'><<</a></li>".format(now_page - 1)  # 上一页
        # else:   # 当前页码不是第一页, 正常显示
        #     before_li = "<li><a href='/page/?page={0}'><<</a></li>".format(now_page - 1)     # 上一页
        # html_list.append(before_li)
        # # 对起始页码到终止页码for循环
        # for num in range(start_page, end_page + 1):
        #     if num == now_page:
        #         li_html = "<li class = 'active'><a href='/page/?page={0}'>{0}</a></li>".format(num)
        #     else:
        #         li_html = "<li><a href='/page/?page={0}'>{0}</a></li>".format(num)
        #     html_list.append(li_html)
        # # 如果当前页码是最后一页, 下一页按键禁用
        # if now_page == page_num:
        #     next_li = "<li class='disabled'><a href='/page/?page={0}'>>></a></li>".format(now_page + 1)  # 下一页
        # else:   # 非最后一页, 正常显示
        #     next_li = "<li><a href='/page/?page={0}'>>></a></li>".format(now_page + 1)       # 下一页
        # html_list.append(next_li)
        # # 加尾页显示按钮
        # last_li = "<li><a href='/page/?page={}'>尾页</a></li>".format(page_num)
        # # 将定义好的标签放入列表
        # html_list.append(last_li)
        # # 将列表使用""组合成字符串
        # html_str = "".join(html_list)
        #
        # # 使用切片的方法将数据放入不同的页面
        # # 切片的起始值
        # start_split = (now_page - 1) * data_num
        # # 终止值
        # end_split = now_page * data_num
      
        return render(request, "page.html", {
            "page_num": range(start_page, end_page +1)    # FBV版
            "html_str": page.show_li()
        })
  • 相关阅读:
    深入浅出SQL教程之Group By和Having
    动网论坛让验证码变清晰得修改方法
    網頁打印代碼大全
    WEB开发必备的强大工具
    SQL Server数据库开发的二十一条军规
    MSDE2000安装:要求使用强SA密码解决方法
    卡巴斯基问题大全
    不加好友也能发消息给对方
    VB中MsFlexGrid控件的使用细则(收集)
    用Setup Factory 7.0制作安装程序 进阶篇
  • 原文地址:https://www.cnblogs.com/dong-/p/9846200.html
Copyright © 2020-2023  润新知