• Django 自定义之分页


    数据源码:

    class Page(object):
        """
        这是一个自定义分页的类
        可以实现Django ORM数据分页展示
    
        使用说明:
            from utils import mypage
            page_obj = mypage.Page(total_num, current_page, 'publisher_list')
            publisher_list = data[page_obj.data_start:page_obj.data_end]
            page_html = page_obj.page_html()
    
            为了显示效果,show_page_num 最好使用奇数
    
        """
    
        def __init__(self, total_num, current_page, url_prefix, per_page=10, show_page_num=11):
            """
    
            :param total_num: 数据的总条数
            :param current_page: 当前访问的页码
            :param url_prefix: 分页代码里a标签的前缀
            :param per_page: 每一页显示多少条数据
            :param show_page_num: 页面上最多显示多少个页码
            """
            self.total_num = total_num
            self.url_prefix = url_prefix
    
            self.per_page = per_page
            self.show_page_num = show_page_num
    
            # 通过初始化传入的值计算的到的值
            total_page, more = divmod(self.total_num, self.per_page)# 展示一半需要的页面
            self.half_show_page_num = self.show_page_num // 2
            # 如果有余数,就把页码数加一
            if more:
                total_page += 1
            self.total_page = total_page
            # 对穿进来的当前页码数做有效检验
            try:
                current_page = int(current_page)
            except Exception as e:
                current_page = 1
            # 如果当前页码数大于 总页码数,默认展示最后一页的数据
            # current_page = total_page if current_page > total_page else current_page
    
            if current_page > self.total_page:
                current_page = self.total_page
            # 如果当前页码数小于1, 默认展示最后一页的数据
            # current_page = 1 if current_page < 1 else current_page
            if current_page < 1:
                current_page = 1
            self.current_page = current_page
    
            # 求 页面上 需要显示的页码范围
            if self.current_page - self.half_show_page_num <= 1:
                page_start = 1
                page_end = self.show_page_num
            elif self.current_page + self.half_show_page_num >= self.total_page:
                page_end = self.total_page
                page_start = self.total_page - self.show_page_num + 1
            else:
                page_start = self.current_page - self.half_show_page_num
                page_end = self.current_page + self.half_show_page_num
            self.page_start = page_start
            self.page_end = page_end   # 我上面一通计算得到的页面显示的页码结束
       

          # 如果你一通计算的得到的页码数比我总共的页码数还多,我就把页码结束指定成我总共有的页码数
          if self.page_end > self.total_page:
          self.page_end = self.total_page

    
    
        @property
        def data_start(self):
            # 返回当前页码应该从哪开始切数据
            return (self.current_page - 1) * self.per_page
    
        @property
        def data_end(self):
            # 返回当前页面应该切到哪里为止
            return self.current_page * self.per_page
    
        def page_html(self):
            li_list = []
            # 添加首页
            li_list.append('<li><a href="/{}/?page=1">首页</a></li>'.format(self.url_prefix))
            # 添加上一页
            if self.current_page <= 1:  # 没有上一页
                prev_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'
            else:
                prev_html = '<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(
                    self.url_prefix,
                    self.current_page - 1)
            li_list.append(prev_html)
            for i in range(self.page_start, self.page_end + 1):
                if i == self.current_page:
                    tmp = '<li class="active"><a href="/{0}/?page={1}">{1}</a></li>'.format(self.url_prefix, i)
                else:
                    tmp = '<li><a href="/{0}/?page={1}">{1}</a></li>'.format(self.url_prefix, i)
                li_list.append(tmp)
            # 添加下一页
            if self.current_page >= self.total_page:  # 表示没有下一页
                next_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'
            else:
                next_html = '<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'.format(
                    self.url_prefix, self.current_page + 1)
            li_list.append(next_html)
            # 添加尾页
            li_list.append('<li><a href="/{}/?page={}">尾页</a></li>'.format(self.url_prefix, self.total_page))
    
            # 将生成的li标签 拼接成一个大的字符串
            page_html = "".join(li_list)
            return page_html
    
    自定义分页源码

    Django  views.py 中使用实例:

    def publisher_list(request):
        # 拿到所有的数据
        data = models.Publisher.objects.all()
        # 统计数据总数
        total_num = data.count()
        # 从URL中拿到当前访问的页码数
        current_page = request.GET.get('page')
        # 导入自定义分页
        from utils import  mypage
        # 实例化出分页对象
        page_obj = mypage.Page(total_num,current_page,'publisher_list')
        # 当前页码应该展示出来的数据
        publisher_list = data[page_obj.data_start:page_obj.data_end]
        # 生成分页的页码(html)
        page_html = page_obj.page_html()
        # 在页面上展示出来
        return render(request,'publisher_list.html',{'publisher_list':publisher_list, 'page_html':page_html})
  • 相关阅读:
    hdu.1226.超级密码(bfs)
    zstu.2512. Moving Tables(贪心)
    zoj.3868.GCD Expectation(数学推导>>容斥原理)
    acdream.LCM Challenge(数学推导)
    acdream.Triangles(数学推导)
    acdream.A Very Easy Triangle Counting Game(数学推导)
    acdream.Bet(数学推导)
    acdream.郭式树(数学推导)
    EntityFramework(转自wiki)
    石墨烯(转自wiki)
  • 原文地址:https://www.cnblogs.com/yangwei666/p/9228833.html
Copyright © 2020-2023  润新知