1. 程式人生 > >Django之分頁升級版

Django之分頁升級版

多少 lse django 顯示 max deepcopy property str ini

如果查詢出的數據,被分頁了,這個時候翻頁的時候應該講頁數的參數和查詢的參數一塊在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之分頁升級版