分页组件(django可用,好用,适用性广)

"""自定义分页组件"""


class Pagination(object):
    def __init__(self, request, queryset, page_param="page", page_size=10, plus=5):
        """
        :param request: 请求对象
        :param queryset: 符合条件的数据以queryset的方式传入
        :param page_param: 在url中传递的参数:/manager/user/list/?query=1
        :param page_size: 每页多少条数据
        :param plus: 分页组件页码显示,默认显示前五个和后五个页码
        """
        page = request.GET.get(page_param, "1")
        from django.http.request import QueryDict
        import copy
        query_dict = copy.deepcopy(request.GET)
        query_dict._mutable = True
        self.page_queryset_dict = query_dict
        if page.isdecimal():
            page = int(page)
        else:
            page = 1
        self.page = page
        self.page_param = page_param
        self.page_size = page_size
        self.start = (page - 1) * page_size
        self.end = page * page_size
        self.page_queryset = queryset[self.start:self.end]
        self.total_count = queryset.count()
        total_page_count, div = divmod(self.total_count, page_size)
        if div:
            total_page_count += 1
        self.total_page_count = total_page_count
        self.plus = plus

    def createhtml(self):
        page_list = []
        from django.utils.safestring import mark_safe
        total_page_count, div = divmod(self.total_count, self.page_size)
        self.page_queryset_dict.setlist(self.page_param, [1])
        first_page = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">首页</span></a></li>'.format(
            self.page_queryset_dict.urlencode())
        page_list.append(first_page)
        if div:
            total_page_count += 1

        if total_page_count <= 2 * self.plus + 1:  # 数据不足11页
            start_page = 1
            end_page = total_page_count
        elif self.page <= self.plus:  # 最小值
            start_page = 1
            end_page = 2 * self.plus + 1
        elif self.page + self.plus > total_page_count:  # 最大值
            start_page = total_page_count - 2 * self.plus
            end_page = total_page_count + 1
        else:
            start_page = self.page - self.plus
            end_page = self.page + self.plus + 1
        self.page_queryset_dict.setlist(self.page_param, [self.page - 1])
        if self.page > 1:
            prev = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(
                self.page_queryset_dict.urlencode())
            page_list.append(prev)
        for i in range(start_page, end_page):
            self.page_queryset_dict.setlist(self.page_param, [i])
            if i == self.page:
                ele = '<li class="active"><a href="?{}">{}</a></li>'.format(self.page_queryset_dict.urlencode(), i)
            else:
                ele = '<li><a href="?{}">{}</a></li>'.format(self.page_queryset_dict.urlencode(), i)
            page_list.append(ele)
        if self.page < total_page_count:
            self.page_queryset_dict.setlist(self.page_param, [self.page + 1])
            nextp = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">»</span></a></li>'.format(
                self.page_queryset_dict.urlencode())
            page_list.append(nextp)

        self.page_queryset_dict.setlist(self.page_param, [total_page_count])
        last_page = '<li><a href="?{}" aria-label="Previous"><span aria-hidden="true">尾页</span></a></li>'.format(
            self.page_queryset_dict.urlencode())

        page_list.append(last_page)
        search_str = """<li>
                        <form method="get" name="page" style="float: left;margin-left: -1px">
                            <div class="input-group">
                                <input type="text" name="page" class="form-control" placeholder="页码"
                                       value="{}"
                                       style="position: relative;float: left;display:inline-block;width: 88px">
                                <button class="btn btn-default" type="submit" style=>跳转</button>
                            </div>
                        </form>
                    </li>""".format(self.page)
        page_list.append(search_str)
        page_string = mark_safe("".join(page_list))
        return page_string

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岩塘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值