django 分页

时光如瑾雨微凉 2020-02-02

已经封装成包,使用时导入即可

实现:

class MyPage:
    def __init__(self, url, info_count, page_num, info_num=10, max_page=11, ):
        """
        :param url: 请求的url
        :param info_count: 总共有多少条数据
        :param page_num: 访问的页码
        :param info_num: 展示多少条数据
        :param max_page: 展示多少条页码
        """

        self.url = url
        self.info_count = info_count
        try:
            page_num = int(page_num)
        except Exception as e:
            page_num = 1
        self.info_num = info_num
        self.page_num = page_num
        self.max_page = max_page
        page_count, m = divmod(self.info_count, self.info_num)
        if m:
            page_count += 1
            self.page_count = page_count
        else:
            self.page_count = page_count

        start_info = (self.page_num - 1) * self.info_num
        end_info = start_info + self.info_num

        self.start_info = start_info
        self.end_info = end_info

        half_max_page = self.max_page // 2
        start_page = self.page_num - half_max_page  # 开始页码
        end_page = self.page_num + half_max_page  # 结束页码

        # 如果可以显示的页码总数少于设置的展示页码数,展示的页码数默认为可以显示的页码总数
        if self.page_count < self.max_page:
            self.start_page = 1
            self.max_page = self.page_count

        # 如果开始页码小于1,则开始页码为1 , 结束页码为最可以显示的页码数
        if start_page < 1:
            start_page = 1
            end_page = self.max_page

        # 如果结束页码大于页码总数,则结束页码为页码总数,开始页码则为页码总数减去规定的可以显示的页码数+1
        if end_page > page_count:
            end_page = page_count
            start_page = end_page - self.max_page + 1

        self.start_page = start_page
        self.end_page = end_page

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

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

    def page_html(self):
        info_list = []
        # 添加一个首页标签
        first_page = ‘<li><a href="{}?next=1">首页</a></li>‘.format(self.url)
        info_list.append(first_page)

        # 添加一个上一页标签,如果是第一页,标签不能点击
        if self.page_num <= 1:
            next_page = ‘<li class="disabled"><a href="{}?next={}" aria-abel="Previous"><spanaria-hidden="true">&laquo;</span></a></li>‘.format(self.url,
                self.page_num)
            info_list.append(next_page)
        else:
            next_page = ‘<li><a href="{}?next={}" aria-abel="Previous"><spanaria-hidden="true">&laquo;</span></a></li>‘.format(self.url, self.page_num - 1)
            info_list.append(next_page)

        for i in range(self.start_page, self.end_page + 1):
            li = ‘<li><a href="{}?next={}">{}</a></li>‘.format(self.url, i, i)
            info_list.append(li)

        # 添加一个下一页标签,如果是最后一页,标签不能点击
        if self.page_num == self.page_count:
            next_page = ‘<li class="disabled"><a href="{}?next={}"><span aria-hidden="true">&raquo;</span></a></li>‘.format(self.url, self.page_num)
            info_list.append(next_page)
        else:
            next_page = ‘<li><a href="{}?next={}"><span aria-hidden="true">&raquo;</span></a></li>‘.format(self.url, self.page_num + 1)
            info_list.append(next_page)

        # 添加一个尾页标签
        trailer_page = ‘<li><a href="{}?next={}">尾页</a></li>‘.format(self.url, self.page_count)
        info_list.append(trailer_page)
        
        str_info_list = ‘‘.join(info_list)  # 将存有页数的HTML标签列表转换成字符串形式
        return str_info_list

分页

用法:

# 导入源码包 
def show_book(request):
    # all_book = models.Book.objects.all()
    # return render(request, ‘show/show_book.html‘, {‘all_book‘: all_book})
    # 总共有多少条数据
    info_count = models.Book.objects.all().count()
    # 获取页码,默认为1
    page_num = request.GET.get(‘next‘, 1)
    # 获取客户端url地址,不包括域名
    url = request.path_info
    # 创建分页对象
    obj_page = paging.MyPage(url=url, info_count=info_count, page_num=page_num, info_num=8, max_page=7)
    # 展示哪些条数据
    info = models.Book.objects.all()
    all_book = info[obj_page.start:obj_page.end]
    # 获取分页HTML代码
    str_info_list = obj_page.page_html()
    return render(request, ‘show/show_book.html‘, {‘all_book‘: all_book, ‘str_info_list‘: str_info_list})

views

相关推荐

inspuryhq / 0评论 2020-07-28