【Python】自定义分页

 Django内置了分页设置,但不够灵活,需要我们自定义分页。

下面代码考虑了利用bootstrap分页组件来实现页面美化,如果不想使用,可以不调用url_format_by_bootstrap函数,即可实现普通分页。

具体的参数和代码均有注释

#! python3
# @Project:  mysite2 - pager.py
# @Info   :  自定义分页操作
import re


class PageInfo:
    """
        :param current_page 当前页码
        :param data 要分页的数据
        :param path 路由地址(用于生成动态路径)
        :param per_page 每页显示条数
        :param show_page 显示页码跨度
    """

    def __init__(self, current_page, data, path, per_page=10, show_page=10):
        self.current_page = current_page
        self.per_page = per_page
        self.show_page = show_page
        self.data = data
        self.path = path
        self.all_page = self.get_all_page()

    def get_all_page(self):
        """计算总页数"""
        result, mod = divmod(len(self.data), self.per_page)
        if mod:
            return result + 1
        else:
            return result

    def create_page(self):
        """创建页码列表"""
        if not (isinstance(self.show_page, int) | self.show_page < 1):
            self.show_page = 10

        half = self.show_page // 2
        start = self.current_page - half
        end = self.current_page + half

        #  避免出现负数页
        if self.current_page - half <= 0:
            start = 1
        if self.current_page + half > self.all_page:
            end = self.all_page
        # 若当前页靠近两端,保持页面达到show_page的数量
        if self.current_page <= half:
            end = self.show_page + 1
        if self.current_page >= self.all_page - half:
            start = self.all_page - self.show_page
        # 上下页
        page_list = []
        pre_page = self.current_page - 1 if self.current_page - 1 > 0 else 1
        next_page = self.current_page + 1 if self.current_page + 1 < self.all_page else self.all_page
        # 将生成的页码标签添加进page_list
        page_list.append(self.get_page_url(self.path, '上一页', pre_page))
        for i in range(start, end + 1):
            url = self.get_page_url(self.path, i, i)
            page_list.append(url)
        page_list.append(self.get_page_url(self.path, '下一页', next_page))

        return self.url_format_by_bootstrap(page_list)

    def per_page_content(self, data):
        """计算每页内容"""
        start_index = (self.current_page - 1) * self.per_page
        end_index = self.current_page * self.per_page
        return data[start_index:end_index]

    #  声明为静态方法,未使用类中变量
    @staticmethod
    def get_page_url(path, text, num):
        """获取动态页码链接"""
        url = '{2}'.format(path, num, text)
        return url

    def url_format_by_bootstrap(self, page_list):
        """将url转化为bootstrap分页需要的格式"""
        pre_url = re.findall('href="(.*?)"', page_list[0])[0]
        next_url = re.findall('href="(.*?)"', page_list[-1])[0]
        page_list[0] = '''
                        
  • '''.format(pre_url) page_list[-1] = '''
  • '''.format(next_url) for index, page in enumerate(page_list[1:-1]): # 将当前页设置为选中状态 page_num = int(re.search(r'page=(\d+)', page).group(1)) if self.current_page == page_num: page_list[index + 1] = '
  • {0}
  • '.format(page) continue page_list[index + 1] = '
  • {0}
  • '.format(page) return page_list

    在view页面只需要传参数就可生成页码

    import logging
    
    from django.http import HttpResponse
    from django.shortcuts import render
    from . import models
    from utils.pager import PageInfo
    
    
    def index(request):
        """首页"""
        try:
            current_page_num = int(request.GET['page'])
        except Exception as e:
            logging.log(logging.ERROR, e)
            current_page_num = 1
        data = models.UserInfo.objects.all()
        page_info = PageInfo(current_page_num, data, '/index')
        page_list = page_info.create_page()
        user_list = page_info.per_page_content(data)
        return render(request, 'index.html', {'user_list': user_list, 'page_list': page_list})

    由于是后端动态生成页码标签,前段直接遍历即可。

    这里应用了bootstrap样式。

    
    
        
            
            
            Title
        
        
            

    用户列表

      {% for row in user_list %}
    • {{ row.username }}
    • {% endfor %}

    具体效果如下:

    【Python】自定义分页_第1张图片

    你可能感兴趣的:(Python)