python写的一段分页的代码

简介: 代码:from django.utils.safestring import mark_safeclass Paginator(object): def __init__(self,current_page,total_item_count,base_url,per_page_cou...

代码:

from django.utils.safestring import mark_safe

class Paginator(object):

    def __init__(self,current_page,total_item_count,base_url,per_page_count=10,show_pager_count=11):
        """
        :param current_page:  当前页码
        :param total_item_count: 数据库数据总条数
        :param base_url: 分页前缀URL
        :param per_page_count:   每页显示数据条数
        :param show_pager_count: 对多显示的页码
        """
        self.current_page = current_page
        self.total_item_count = total_item_count
        self.base_url = base_url
        self.per_page_count = per_page_count
        self.show_pager_count = show_pager_count

        #获取页码数及最后一页上显示的条目数
        max_pager_num, b = divmod(total_item_count, per_page_count)

        if b:
            max_pager_num += 1
        self.max_pager_num = max_pager_num

    @property
    def start(self):
        """
        #每一页显示的第一条数据
        :return:
        """
        return (self.current_page-1)* self.per_page_count

    @property
    def end(self):
        """
        #每一页显示的最后一条数据
        :return:
        """
        return self.current_page * self.per_page_count

    def page_html(self):
        """

        :return:
        """
        page_list = []

        #如果当前页为第1页,则上一页按钮不可用
        if self.current_page == 1:
            prev = ' <li><a href="#">上一页</a></li>'
        else:
            prev = ' <li><a href="%s?page=%s">上一页</a></li>' % (self.base_url,self.current_page - 1,)
        page_list.append(prev)

        half_show_pager_count = int(self.show_pager_count / 2)

        # 页面显示的总页数小于定义的页面上显示的页数时
        if self.max_pager_num < self.show_pager_count:
            pager_start = 1
            pager_end = self.max_pager_num + 1
        else:
            #当前页码数小于定义的页面显示的页数的一半时
            if self.current_page <= half_show_pager_count:
                pager_start = 1
                pager_end = self.show_pager_count + 1
            else:
                #当前面码数大于定义的页面显示的页数的一半时
                if self.current_page + half_show_pager_count > self.max_pager_num:
                    pager_start = self.max_pager_num - self.show_pager_count + 1
                    pager_end = self.max_pager_num + 1
                else:
                    #正常显示的时候
                    pager_start = self.current_page - half_show_pager_count
                    pager_end = self.current_page + half_show_pager_count + 1

        #遍历循环当前页的每一条记录
        for i in range(pager_start, pager_end):
            if i == self.current_page:
                tpl = ' <li class="active"><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
            else:
                tpl = ' <li><a href="%s?page=%s">%s</a></li>' % (self.base_url,i, i,)
            page_list.append(tpl)

        # 如果当前页为最后一页,则下一页按钮不可用
        if self.current_page == self.max_pager_num:
            next = ' <li><a href="#">下一页</a></li>'
        else:
            next = ' <li><a href="%s?page=%s">下一页</a></li>' % (self.base_url,self.current_page + 1,)
        page_list.append(next)

        return mark_safe(''.join(page_list))

使用方法:

从浏览器中取出当前的页码数

current_page=int(request.GET.get("page",1))

从数据库中取出的总的记录数

item_list=models.数据表.objects.all()
total_item_count=item_list.count()

使用Paginator类实例化一个页码的对象:

page_obj=Paginator(current_page,total_item_count,"/index/")
需要注意的是:

    实例化page_obj的时候,可以定义每页显示的记录个数per_page_count及显示在页面上的页码的个数show_page_count

    每页显示的记录数per_page_count默认值为10,
    页面显示的页码的个数show_page_count默认值为11

定义返回到客户端的记录列表

item_list=models.数据表.objects.all()[page_obj.start:page_obj.end]

最后使用render或者redirect返回给客户端

return render(request,"aaa.html",{"item_list":item_list,"page_html":page_obj.page_html()})
目录
相关文章
|
19天前
|
存储 算法 调度
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
【复现】【遗传算法】考虑储能和可再生能源消纳责任制的售电公司购售电策略(Python代码实现)
126 26
|
22天前
|
测试技术 开发者 Python
Python单元测试入门:3个核心断言方法,帮你快速定位代码bug
本文介绍Python单元测试基础,详解`unittest`框架中的三大核心断言方法:`assertEqual`验证值相等,`assertTrue`和`assertFalse`判断条件真假。通过实例演示其用法,帮助开发者自动化检测代码逻辑,提升测试效率与可靠性。
149 1
|
25天前
|
机器学习/深度学习 算法 调度
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
基于多动作深度强化学习的柔性车间调度研究(Python代码实现)
103 1
|
8天前
|
测试技术 Python
Python装饰器:为你的代码施展“魔法”
Python装饰器:为你的代码施展“魔法”
194 100
|
8天前
|
开发者 Python
Python列表推导式:一行代码的艺术与力量
Python列表推导式:一行代码的艺术与力量
170 95
|
16天前
|
Python
Python的简洁之道:5个让代码更优雅的技巧
Python的简洁之道:5个让代码更优雅的技巧
171 104
|
16天前
|
开发者 Python
Python神技:用列表推导式让你的代码更优雅
Python神技:用列表推导式让你的代码更优雅
293 99
|
8天前
|
缓存 Python
Python装饰器:为你的代码施展“魔法
Python装饰器:为你的代码施展“魔法
125 88
|
23天前
|
IDE 开发工具 开发者
Python类型注解:提升代码可读性与健壮性
Python类型注解:提升代码可读性与健壮性
195 102
|
13天前
|
监控 机器人 编译器
如何将python代码打包成exe文件---PyInstaller打包之神
PyInstaller可将Python程序打包为独立可执行文件,无需用户安装Python环境。它自动分析代码依赖,整合解释器、库及资源,支持一键生成exe,方便分发。使用pip安装后,通过简单命令即可完成打包,适合各类项目部署。

推荐镜像

更多