分頁器組件支持保存搜索條件
代碼:
1 class Pagination(object): 2 def __init__(self, current_page, all_count, params,per_page_num=10, pager_count=11): 3 """ 4 封裝分頁相關數據 5 :param current_page: 當前頁 6 :param all_count: 數據庫中的數據總條數 7 :param per_page_num: 每頁顯示的數據條數 8 :param pager_count: 最多顯示的頁碼個數 9 10 用法: 11 queryset = model.objects.all() 12 page_obj = Pagination(current_page,all_count) 13 page_data = queryset[page_obj.start:page_obj.end] 14 獲取數據用page_data而不再使用原始的queryset 15 獲取前端分頁樣式用page_obj.page_html 16 17 示例代碼: 18 current_page = self.request.GET.get("page", 1) 19 self.page_obj = Pagination(current_page=current_page,all_count=self.queryset.count(),params=self.request.GET) 20 self.page_queryset = self.queryset[self.page_obj.start:self.page_obj.end] 21 前臺:{{ show_obj.page_obj.page_html|safe }} 22 """ 23 try: 24 current_page = int(current_page) 25 except Exception as e: 26 current_page = 1 27 28 if current_page < 1: 29 current_page = 1 30 31 self.current_page = current_page 32 33 self.all_count = all_count 34 self.per_page_num = per_page_num 35 36 # 總頁碼 37 all_pager, tmp = divmod(all_count, per_page_num) 38 if tmp: 39 all_pager += 1 40 self.all_pager = all_pager 41 42 self.pager_count = pager_count 43 self.pager_count_half = int((pager_count - 1) / 2) 44 45 import copy 46 self.params = copy.deepcopy(params) 47 48 @property 49 def start(self): 50 return (self.current_page - 1) * self.per_page_num 51 52 @property 53 def end(self): 54 return self.current_page * self.per_page_num 55 56 def page_html(self): 57 # 如果總頁碼 < 11個: 58 if self.all_pager <= self.pager_count: 59 pager_start = 1 60 pager_end = self.all_pager + 1 61 # 總頁碼 > 11 62 else: 63 # 當前頁如果<=頁面上最多顯示11/2個頁碼 64 if self.current_page <= self.pager_count_half: 65 pager_start = 1 66 pager_end = self.pager_count + 1 67 68 # 當前頁大於5 69 else: 70 # 頁碼翻到最後 71 if (self.current_page + self.pager_count_half) > self.all_pager: 72 pager_end = self.all_pager + 1 73 pager_start = self.all_pager - self.pager_count + 1 74 else: 75 pager_start = self.current_page - self.pager_count_half 76 pager_end = self.current_page + self.pager_count_half + 1 77 78 page_html_list = [] 79 # 添加前面的nav和ul標籤 80 page_html_list.append(''' 81 <nav aria-label='Page navigation>' 82 <ul class='pagination'> 83 ''') 84 first_page = '<li><a href="?page=%s">首頁</a></li>' % (1) 85 page_html_list.append(first_page) 86 87 if self.current_page <= 1: 88 prev_page = '<li class="disabled"><a href="#">上一頁</a></li>' 89 else: 90 prev_page = '<li><a href="?page=%s">上一頁</a></li>' % (self.current_page - 1,) 91 92 page_html_list.append(prev_page) 93 94 for i in range(pager_start, pager_end): 95 self.params['page'] = i 96 if i == self.current_page: 97 temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.params.urlencode(), i,) 98 else: 99 temp = '<li><a href="?%s">%s</a></li>' % (self.params.urlencode(), i,) 100 page_html_list.append(temp) 101 102 if self.current_page >= self.all_pager: 103 next_page = '<li class="disabled"><a href="#">下一頁</a></li>' 104 else: 105 next_page = '<li><a href="?page=%s">下一頁</a></li>' % (self.current_page + 1,) 106 page_html_list.append(next_page) 107 108 last_page = '<li><a href="?page=%s">尾頁</a></li>' % (self.all_pager,) 109 page_html_list.append(last_page) 110 # 尾部添加標籤 111 page_html_list.append(''' 112 </nav> 113 </ul> 114 ''') 115 return ''.join(page_html_list)