码迷,mamicode.com
首页 > 其他好文 > 详细

Django之分页升级版

时间:2018-10-24 22:14:29      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:return   from   分页器   ini   数据量   pre   ext   append   nbsp   

 

如果查询出的数据,被分页了,这个时候翻页的时候应该讲页数的参数和查询的参数一块在URL上显示

"""
分页器
"""

from django.utils.safestring import mark_safe
from django.http import QueryDict


class Pagination:

    # request 为request请求, all_count为所有数据的个数, query_params为查询的时候将查询结果与页数进行拼接, per_num为一页展示多少数据, max_show分多少页
    def __init__(self, request, all_count, query_params=QueryDict(), per_num=10, max_show=11):
        # 基本的URL
        self.base_url = request.path_info
        # 查询条件
        self.query_params = query_params
        self.query_params._mutable = True
        # 当前页码
        try:
            self.current_page = int(request.GET.get(page, 1))
            if self.current_page <= 0:
                self.current_page = 1
        except Exception as e:
            self.current_page = 1
            print(e)
        # 最多显示的页码数
        self.max_show = max_show
        half_show = max_show // 2

        # 每页显示的数据条数
        self.per_num = per_num
        # 总数据量
        self.all_count = all_count

        # 总页码数
        self.total_num, more = divmod(all_count, per_num)
        if more:
            self.total_num += 1

        # 总页码数小于最大显示数:显示总页码数
        if self.total_num <= max_show:
            self.page_start = 1
            self.page_end = self.total_num
        else:
            # 总页码数大于最大显示数:最多显示11个
            if self.current_page <= half_show:
                self.page_start = 1
                self.page_end = max_show
            elif self.current_page + half_show >= self.total_num:
                self.page_end = self.total_num
                self.page_start = self.total_num - max_show + 1
            else:
                self.page_start = self.current_page - half_show
                self.page_end = self.current_page + half_show

    @property
    def start(self):
        return (self.current_page - 1) * self.per_num

    @property
    def end(self):
        return self.current_page * self.per_num

    @property
    def show_li(self):
        # 存放li标签的列表
        html_list = []

        self.query_params[page] = 1
        # query=a&page=1

        first_li = <li><a href="{}?{}">首页</a></li>.format(self.base_url, self.query_params.urlencode())
        html_list.append(first_li)

        if self.current_page == 1:
            prev_li = <li class="disabled"><a><<</a></li>
        else:
            self.query_params[page] = self.current_page - 1
            prev_li = <li><a href="{0}?{1}"><<</a></li>.format(self.base_url, self.query_params.urlencode())
        html_list.append(prev_li)

        for num in range(self.page_start, self.page_end + 1):
            self.query_params[page] = num
            if self.current_page == num:
                li_html = <li class="active"><a href="{0}?{1}">{2}</a></li>.format(self.base_url,
                                                                                     self.query_params.urlencode(), num)
            else:
                li_html = <li><a href="{0}?{1}">{2}</a></li>.format(self.base_url,
                                                                       self.query_params.urlencode(), num)
            html_list.append(li_html)

        if self.current_page == self.total_num:
            next_li = <li class="disabled"><a>>></a></li>
        else:
            self.query_params[page] = self.current_page + 1
            next_li = <li><a href="{0}?{1}">>></a></li>.format(self.base_url, self.query_params.urlencode())

        html_list.append(next_li)

        self.query_params[page] = self.total_num
        last_li = <li><a href="{}?{}">尾页</a></li>.format(self.base_url, self.query_params.urlencode())
        html_list.append(last_li)

        return mark_safe(‘‘.join(html_list))

 

 

 

# 解决搜索后的url翻页拼接问题
        print(query, request.GET)  # <QueryDict: {‘query‘: [结果]}>
        # query=结果
        print(request.GET.urlencode())
        # _mutable=True  这样就可以修改了
        # query_params = copy.deepcopy(request.GET)
        query_params = request.GET.copy()  # 为了将查询结果与页数都放到url上
        # query_params._mutable = True

        page = pagination.Pagination(request, customer.count(), query_params, per_num=3)
        return render(request, crm/customer_list.html, {
            "customer": customer[page.start:page.end],
            html_str: page.show_li,
        })

 

Django之分页升级版

标签:return   from   分页器   ini   数据量   pre   ext   append   nbsp   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/9846023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!