分页功能的实现

简介: 直接上代码吧:基础版import mathclass Page: def __init__(self, total_num, page_num, every_page_num=10, show_page_num=9): ...

直接上代码吧:

基础版

import math


class Page:

    def __init__(self, total_num, page_num, every_page_num=10, show_page_num=9):
        """
        :param total_num: 数据库获取到的数据的总数
        :param page_num: 前端获取的页数
        :param every_page_num: 自定义每页显示条数
        :param show_page_num: 显示分页数目
        """
        self.total_num = total_num
        self.page_num = page_num
        self.every_page_num = every_page_num
        self.show_page_num = show_page_num
        self.page_tags_html = ''
        self.page_start_num = 0
        self.page_end_num = 0
        self.start_page_num = 1
        self.end_page_num = 1
        try:
            self.page_num = int(self.page_num)
        except:
            self.page_num = 1
        # 通过向上取整计算出数据的总页数
        self.total_page_num = math.ceil(self.total_num / self.every_page_num)
        if self.total_page_num:
            # 判断从前端得到的页码数是否小于1,小于1就重置为1
            if self.page_num < 1:
                self.page_num = 1
            # 判断从前端得到的页码数是否大于总页码数,大于就重置为最大页码数
            elif self.page_num > self.total_page_num:
                self.page_num = self.total_page_num
            # 当前页的开始数据
            self.page_start_num = (self.page_num - 1) * self.every_page_num
            # 当前页的结束数据
            self.page_end_num = (self.page_num) * self.every_page_num
            # 判断总页码数和要展示的页数,如果总页码数大于要展示的页码数就展示对应的页数否则有多少页就展示多少页
            if self.total_page_num > self.show_page_num:
                self.start_page_num = self.page_num - (self.show_page_num // 2)
                self.end_page_num = self.page_num + (self.show_page_num // 2)
                if self.start_page_num < 1:
                    self.start_page_num = 1
                    self.end_page_num = self.show_page_num
                if self.end_page_num > self.total_page_num:
                    self.end_page_num = self.total_page_num
                    self.start_page_num = (self.total_page_num - self.show_page_num)
            else:
                self.start_page_num = 1
                self.end_page_num = self.total_page_num

    @property
    def start(self):
        return self.page_start_num

    @property
    def end(self):
        return self.page_end_num

    @property
    def html(self):
        pre_page_num = self.page_num - 1 if self.page_num > 1 else self.page_num
        next_page_num = self.page_num + 1 if self.page_num < self.total_page_num else self.total_page_num
        The_first_page = '<li><a href="?page=1">首页</a>' \
                         f'</li><li><a href="?page={pre_page_num}">&laquo;</a></li>'
        The_last_page = f'<li><a href="?page={next_page_num}">&raquo;</a></li>' \
                        f'<li><a href="?page={self.total_page_num}">尾页</a></li></ul></nav>'

        self.page_tags_html = f'<nav aria-label="Page navigation"> <ul class="pagination">{The_first_page}'
        for i in range(self.start_page_num, self.end_page_num + 1):
            if i == self.page_num:
                self.page_tags_html += f'<li class="active"><a href="?page={i}">{i}</a></li>'
            else:
                self.page_tags_html += f'<li><a href="?page={i}">{i}</a></li>'
        self.page_tags_html += The_last_page
        return self.page_tags_html

高级版(增加了url参数的保留功能)

import math
import copy

class Page:
    def __init__(self, total_num, page_num, request, every_page_num=10, show_page_num=9):
        """
        :param total_num: 数据库获取到的数据的总数
        :param request: 当前url请求数据,用于获取url参数
        :param page_num: 前端获取的页数
        :param every_page_num: 自定义每页显示条数
        :param show_page_num: 显示分页数目
        """
        self.total_num = total_num
        self.page_num = page_num
        self.every_page_num = every_page_num
        self.show_page_num = show_page_num
        self.page_tags_html = ''
        self.page_start_num = 0
        self.page_end_num = 0
        self.start_page_num = 1
        self.end_page_num = 1

        try:
            self.page_num = int(self.page_num)
        except:
            self.page_num = 1

        # 获取当前url请求中的参数
        params = copy.deepcopy(request.GET)
        self.params = params
        # 通过向上取整计算出数据的总页数
        self.total_page_num = math.ceil(self.total_num / self.every_page_num)
        if self.total_page_num:
            # 判断从前端得到的页码数是否小于1,小于1就重置为1
            if self.page_num < 1:
                self.page_num = 1
            # 判断从前端得到的页码数是否大于总页码数,大于就重置为最大页码数
            elif self.page_num > self.total_page_num:
                self.page_num = self.total_page_num
            # 当前页的开始数据
            self.page_start_num = (self.page_num - 1) * self.every_page_num
            # 当前页的结束数据
            self.page_end_num = (self.page_num) * self.every_page_num
            # 判断总页码数和要展示的页数,如果总页码数大于要展示的页码数就展示对应的页数否则有多少页就展示多少页
            if self.total_page_num > self.show_page_num:
                self.start_page_num = self.page_num - (self.show_page_num // 2)
                self.end_page_num = self.page_num + (self.show_page_num // 2)
                if self.start_page_num < 1:
                    self.start_page_num = 1
                    self.end_page_num = self.show_page_num
                if self.end_page_num > self.total_page_num:
                    self.end_page_num = self.total_page_num
                    self.start_page_num = (self.total_page_num - self.show_page_num)
            else:
                self.start_page_num = 1
                self.end_page_num = self.total_page_num

    @property
    def start(self):
        return self.page_start_num

    @property
    def end(self):
        return self.page_end_num

    @property
    def html(self):

        pre_page_num = self.page_num - 1 if self.page_num > 1 else self.page_num
        next_page_num = self.page_num + 1 if self.page_num < self.total_page_num else self.total_page_num
        self.params["page"] = 1
        The_home_page_html = f'<li><a href="?{self.params.urlencode()}">首页</a></li>'
        self.params["page"] = pre_page_num
        pre_page_num_html = f'<li><a href="?{self.params.urlencode()}">&laquo;</a></li>'
        The_first_page_html = The_home_page_html + pre_page_num_html
        self.params["page"] = next_page_num
        next_page_num_html = f'<li><a href="?{self.params.urlencode()}">&raquo;</a></li>'
        self.params["page"] = self.total_page_num
        The_end_page_html = f'<li><a href="?{self.params.urlencode()}">尾页</a></li></ul></nav>'
        The_last_page_html = next_page_num_html + The_end_page_html
        self.page_tags_html = f'<nav aria-label="Page navigation"> <ul class="pagination">{The_first_page_html}'
        for i in range(self.start_page_num, self.end_page_num + 1):
            self.params["page"] = i
            if i == self.page_num:
                self.page_tags_html += f'<li class="active"><a href="?{self.params.urlencode()}">{i}</a></li>'
            else:
                self.page_tags_html += f'<li><a href="?{self.params.urlencode()}">{i}</a></li>'
        self.page_tags_html += The_last_page_html

        return self.page_tags_html

分页的使用:

class Home(views.View):
    def get(self, request):
        article_list = models.Article.objects.all()
        totol_num = article_list.count()
        page_num = request.GET.get("page", 1)
        # 分页实例化传参
        page_obj = Page(total_num=totol_num, page_num=page_num, every_page_num=9)
        # 根据分页拿到的start和end切片得到所要的数据
        data = article_list[page_obj.start:page_obj.end]
        # 调用html方法得到分页的html代码片段
        page_html = page_obj.html
        return render(request, 'home.html', {'article_list': data, 'page_html': page_html})

ps:在python脚本中调用django环境实现数据库批量添加数据操作

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fenye.settings")

if __name__ == "__main__":
    import django
    django.setup()
    from app01 import models


    def create():
        obj_list = [models.Book(name = f"pyhton{i}") for i in range(160)]
        models.Book.objects.bulk_create(obj_list)

    create()
    #
    # def delete():
    #     models.Book.objects.all().delete()

    # delete()
相关文章
|
2月前
|
前端开发
bladex自定义分页
bladex自定义分页
|
9月前
|
SQL 前端开发 Java
通用分页-后台
通用分页-后台
76 0
|
2月前
分页实现
分页实现
20 0
|
10月前
|
算法 JavaScript Java
通用分页【下】(将分页封装成标签)
调试()是指在软件开发过程中,通过识别、定位和解决程序错误或问题的过程。调试的目的是找出代码中的错误、异常或不正常的行为,并修复它们,以确保程序能够按照预期的方式运行。调试是一个重要的开发技巧,可以帮助开发人员理解程序的执行过程、找出错误的原因,并从中学习和改进。调试可以使用不同的工具和技术来辅助,例如打印输出、日志记录、调试器(debugger)等。调试是开发过程中不可或缺的一部分,可以帮助开发人员提高代码质量、加快解决问题的速度,并优化程序的性能和可靠性。
|
2月前
|
SQL Oracle 关系型数据库
3.分页
3.分页
|
SQL Oracle 关系型数据库
什么是分页?如何使用分页?(一)
什么是分页?如何使用分页?
134 0
|
SQL 存储 关系型数据库
什么是分页?如何使用分页?(二)
什么是分页?如何使用分页?
57 0
|
JSON API 数据库
分页
1. program中注册`builder.Services.AddSingleton<IActionContextAccessor, ActionContextAccessor>();` 2. 控制器中增加依赖
|
NoSQL 大数据 MongoDB
分页控制|学习笔记
快速学习分页控制
54 0
|
SQL Java 数据库连接
分页的实现
分页的实现