Python - 自定义分页

2021-02-10 10:19

阅读:549

标签:href   索引   rev   text   ram   对象   models   判断   数据库   

"""
使用流程
分页组件应用:
1. 在视图函数中
    queryset = models.Issues.objects.filter(project_id=project_id)
    page_object = Pagination(
        current_page=request.GET.get(‘page‘),
        all_count=queryset.count(),
        base_url=request.path_info,
        query_params=request.GET
    )
    issues_object_list = queryset[page_object.start:page_object.end]

    context = {
        ‘issues_object_list‘: issues_object_list,
        ‘page_html‘: page_object.page_html()
    }
    return render(request, ‘issues.html‘, context)
2. 前端
    {% for item in issues_object_list %}
        {{item.xxx}}
    {% endfor %}

     
"""
class Pagination(object):
    def __init__(self, current_page, all_count, base_url, query_params, per_page=30, pager_page_count=11):
        """
        分页初始化
        :param current_page: 当前页码
        :param per_page: 每页显示数据条数
        :param all_count: 数据库中总条数
        :param base_url: 基础URL
        :param query_params: QueryDict对象,内部含所有当前URL的原条件
        :param pager_page_count: 页面上最多显示的页码数量
        """
        self.base_url = base_url
        try:
            self.current_page = int(current_page)
            if self.current_page  总页码
                if (self.current_page + self.half_pager_page_count) > self.pager_count:
                    pager_end = self.pager_count
                    pager_start = self.pager_count - self.pager_page_count + 1
                else:
                    pager_start = self.current_page - self.half_pager_page_count
                    pager_end = self.current_page + self.half_pager_page_count

        page_list = []

        if self.current_page 上一页‘
        else:
            self.query_params[‘page‘] = self.current_page - 1
            prev = ‘
  • 上一页
  • ‘ % (self.base_url, self.query_params.urlencode()) page_list.append(prev) for i in range(pager_start, pager_end + 1): self.query_params[‘page‘] = i if self.current_page == i: tpl = ‘
  • %s
  • ‘ % ( self.base_url, self.query_params.urlencode(), i,) else: tpl = ‘
  • %s
  • ‘ % (self.base_url, self.query_params.urlencode(), i,) page_list.append(tpl) if self.current_page >= self.pager_count: nex = ‘
  • 下一页
  • ‘ else: self.query_params[‘page‘] = self.current_page + 1 nex = ‘
  • 下一页
  • ‘ % (self.base_url, self.query_params.urlencode(),) page_list.append(nex) if self.all_count: tpl = "
  • 共%s条数据,页码%s/%s页
  • " % ( self.all_count, self.current_page, self.pager_count,) page_list.append(tpl) page_str = "".join(page_list) return page_str

    Python - 自定义分页

    标签:href   索引   rev   text   ram   对象   models   判断   数据库   

    原文地址:https://www.cnblogs.com/zhaoganggang/p/12743627.html


    评论


    亲,登录后才可以留言!