自定義分頁器方式一,效果圖以下:javascript
代碼以下:html
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Xiaobai Lei import copy class MyPagination(object): """自定義分頁器""" def __init__(self, current_page_num, all_count, request, per_page_num=10, pager_show_count=9): """ :param current_page_num: 當前用戶選擇的頁數 :param all_count: 數據庫中數據的總數 :param request: 用戶提交的數據集合(主要是要拿用戶請求的參數) :param per_page_num: 每頁顯示的條數 :param pager_show_count: 最多顯示的頁碼個數 """ try: # 若是用戶輸入的是非數字,則默認顯示第一頁 current_page_num = int(current_page_num) except Exception as e: current_page_num = 1 if current_page_num < 1: # 若是小於1頁碼無效,也返回第一頁 current_page_num = 1 self.current_page_num = current_page_num self.all_count = all_count self.per_page_num = per_page_num self.url_params = copy.deepcopy(request.GET) # 計算獲得總頁數 all_count_pager, tmp = divmod(all_count, per_page_num) if tmp: all_count_pager += 1 self.all_count_pager = all_count_pager self.pager_show_count = pager_show_count self.pager_show_half_count = (self.pager_show_count - 1) // 2 @property def start(self): """當前頁數據切片的起點""" return (self.current_page_num - 1) * self.per_page_num @property def end(self): """當前頁數據切片的終點""" return self.current_page_num * self.per_page_num def page_main(self): """根據用戶自定義顯示分頁條""" # 若是總頁數小於最多顯示的頁碼個數,就顯示所有 if self.all_count_pager <= self.pager_show_count: pager_start = 1 pager_end = self.all_count_pager + 1 else: # 當頁面若是<=頁面頁碼一半的時候,頁面頁碼不發生變化 if self.current_page_num <= self.pager_show_half_count: pager_start = 1 pager_end = self.pager_show_count + 1 else: # 當頁碼翻到最後時 if (self.current_page_num + self.pager_show_half_count) > self.all_count_pager: pager_start = self.all_count_pager - self.pager_show_count + 1 pager_end = self.all_count_pager + 1 else: pager_start = self.current_page_num - self.pager_show_half_count pager_end = self.current_page_num + self.pager_show_half_count + 1 # 存放分頁的全部html標籤 page_html_list = [] self.url_params['page'] = 1 first_page = '<li><a href="?%s">首頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(first_page) if self.current_page_num <= 1: prev_page = '<li class="disabled"><a href="javascript:void(0);">上一頁</a></li>' else: self.url_params['page'] = self.current_page_num - 1 prev_page = '<li><a href="?%s">上一頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(prev_page) for i in range(pager_start, pager_end): self.url_params['page'] = i if i == self.current_page_num: temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.url_params.urlencode(), i) else: temp = '<li><a href="?%s">%s</a></li>' % (self.url_params.urlencode(), i) page_html_list.append(temp) if self.current_page_num >= self.all_count_pager: next_page = '<li class="disabled"><a href="javascript:void(0);">下一頁</a></li>' else: self.url_params['page'] = self.current_page_num + 1 next_page = '<li><a href="?%s">下一頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(next_page) self.url_params['page'] = self.all_count_pager last_page = '<li><a href="?%s">尾頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(last_page) return ''.join(page_html_list)
方式二,效果圖以下:java
我的比較喜歡第二種,固然還能夠在後面加個用戶輸入第幾頁就跳轉第幾頁,代碼以下:python
#!/usr/bin/env python # -*- coding:utf-8 -*- # Author: Xiaobai Lei import copy class MyPagination(object): """自定義分頁器""" def __init__(self, current_page_num, all_count, request, per_page_num=10, pager_show_count=9): """ :param current_page_num: 當前用戶選擇的頁數 :param all_count: 數據庫中數據的總數 :param request: 用戶提交的數據集合(主要是要拿用戶請求的參數) :param per_page_num: 每頁顯示的條數 :param pager_show_count: 最多顯示的頁碼個數 """ try: # 若是用戶輸入的是非數字,則默認顯示第一頁 current_page_num = int(current_page_num) except Exception as e: current_page_num = 1 if current_page_num < 1: # 若是小於1頁碼無效,也返回第一頁 current_page_num = 1 self.current_page_num = current_page_num self.all_count = all_count self.per_page_num = per_page_num self.url_params = copy.deepcopy(request.GET) # 計算獲得總頁數 all_count_pager, tmp = divmod(all_count, per_page_num) if tmp: all_count_pager += 1 self.all_count_pager = all_count_pager self.pager_show_count = pager_show_count self.pager_show_half_count = (self.pager_show_count - 1) // 2 @property def start(self): """當前頁數據切片的起點""" return (self.current_page_num - 1) * self.per_page_num @property def end(self): """當前頁數據切片的終點""" return self.current_page_num * self.per_page_num def page_main(self): """根據用戶自定義顯示分頁條""" page_num_list = [] # 若是總頁數小於最多顯示的頁碼個數,就顯示所有 if self.all_count_pager <= self.pager_show_count: page_num_list = list(range(1, self.all_count_pager + 1)) else: # 當前頁面若是<=頁面頁碼一半的時候,頁面頁碼不發生變化 if self.current_page_num <= self.pager_show_half_count + 1: page_num_list = list(range(1, self.pager_show_count)) page_num_list.append('...') page_num_list.append(self.all_count_pager) else: # 當頁碼翻到最後時 if (self.current_page_num + self.pager_show_half_count) >= self.all_count_pager: page_num_list.append(1) page_num_list.append('...') page_num_list.extend(list(range(self.all_count_pager - self.pager_show_count + 1 + 1 , self.all_count_pager + 1))) else: page_num_list.append(1) page_num_list.append('...') page_num_list.extend(list(range(self.current_page_num - self.pager_show_half_count +1, self.current_page_num + self.pager_show_half_count ))) page_num_list.append('...') page_num_list.append(self.all_count_pager) # 存放分頁的全部html標籤 page_html_list = [] if self.current_page_num <= 1: prev_page = '<li class="disabled"><a href="javascript:void(0);">上一頁</a></li>' else: self.url_params['page'] = self.current_page_num - 1 prev_page = '<li><a href="?%s">上一頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(prev_page) for i in page_num_list: if i == "...": temp = '<li class="disabled"><a href="javascript:void(0);">%s</a></li>' % (i) page_html_list.append(temp) continue self.url_params['page'] = i if i == self.current_page_num: temp = '<li class="active"><a href="?%s">%s</a></li>' % (self.url_params.urlencode(), i) else: temp = '<li><a href="?%s">%s</a></li>' % (self.url_params.urlencode(), i) page_html_list.append(temp) if self.current_page_num >= self.all_count_pager: next_page = '<li class="disabled"><a href="javascript:void(0);">下一頁</a></li>' else: self.url_params['page'] = self.current_page_num + 1 next_page = '<li><a href="?%s">下一頁</a></li>' % (self.url_params.urlencode()) page_html_list.append(next_page) return ''.join(page_html_list)
在試圖中使用以下:數據庫
from bms.myfunction import MyPagination # 在函數或類中 current_page_num = request.GET.get('page') paginator = MyPagination(current_page_num, book_list.count(), request, per_page_num=10, pager_show_count=9) book_list = book_list[paginator.start:paginator.end] return render(request, "bms/books/book.html",{'book_list': book_list,'paginator':paginator,'publish_list': publish_list,"author_list":author_list})
在templates下html能夠這麼寫,用到了bootstrap樣式:bootstrap
<nav aria-label="Page navigation" style="text-align: center"> <ul class="pagination" style="margin-top: 0;"> {{ paginator.page_main|safe }} </ul> </nav>