Django之分頁升級版

 

若是查詢出的數據,被分頁了,這個時候翻頁的時候應該講頁數的參數和查詢的參數一塊在URL上顯示html

"""
分頁器
"""

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,
        })
相關文章
相關標籤/搜索