Django 自定義分頁類

分頁類代碼:html

class Page(object):
'''
自定義分頁類
能夠實現Django ORM數據的的分頁展現
輸出HTML代碼:

使用說明:
from utils import mypage
page_obj = mypage.Page(total_num, current_page, 'publisher_list')
publisher_list = data[page_obj.data_start:page_obj.data_end]
page_html = page_obj.page_html()

爲了顯示效果,show_page_num最好使用奇數
'''

def __init__(self, total_num, current_page, url_prefix, per_page=10, show_page_num=9):
'''

:param total_num: 數據總條數
:param current_page: 當前訪問的頁碼
:param url_prefix: 分頁代碼裏a標籤的前綴
:param per_page: 每一頁顯示多少數據
:param show_page_num: 頁面上最多顯示多少個頁碼


'''
self.total_num = total_num
self.url_prefix = url_prefix
self.per_page = per_page
self.show_page_num = show_page_num
# 根據傳入的值計算當前頁碼左右放置多少個頁碼
self.half_show_page_num = self.show_page_num // 2
# 計算當前數據總共須要多少頁碼
total_page, more = divmod(self.total_num, self.per_page)
# 若是有餘數就把頁碼加1
if more:
total_page += 1
self.total_page = total_page
# 數據有效性校驗
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
# 若是當前頁碼數大於總頁碼數,默認展現最後一頁數據
if current_page > self.total_page:
current_page = self.total_page
# 若是當前頁碼小於1,默認展現第一頁數據
if current_page < 1:
current_page = 1
self.current_page = current_page
# 計算頁面上須要展現的頁碼範圍
# 頁碼太小處理
if self.current_page - self.half_show_page_num <= 1:
page_start = 1
page_end = show_page_num
# 頁碼過大處理
elif self.current_page + self.half_show_page_num >= self.total_page:
page_end = self.total_page
page_start = self.total_page - self.show_page_num + 1
# 未超過正常範圍
else:
page_start = self.current_page - self.half_show_page_num
page_end = self.current_page + self.half_show_page_num

# 計算獲得的最終頁碼數
self.page_start = page_start
self.page_end = page_end
# 若是計算獲得的頁碼數比總共須要展現頁碼數多,則把頁碼結束指定爲總頁碼數
if self.page_end > self.total_page:
self.page_end = self.total_page

@property
def data_start(self):
'''

:return: 返回當前頁面應該從哪裏開始切數據
'''
return (self.current_page - 1) * self.per_page

@property
def data_end(self):
'''

:return: 返回當前頁面從哪裏結束數據
'''
return self.current_page * self.per_page

def page_html(self):
li_list = []
# 添加前面的nav和ul標籤
li_list.append("""
<nav aria-label="Page navigation">
<ul class="pagination">
""")
# 添加首頁
li_list.append('<li><a href="/{}/?page=1">首頁</a></li>'.format(self.url_prefix))
# 添加上一頁
if self.current_page <= 1: # 沒有上一頁
prev_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'
else:
prev_html = '<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>'.format(
self.url_prefix,
self.current_page - 1)
li_list.append(prev_html)
# 循環添加內部全部的頁碼
for i in range(self.page_start, self.page_end + 1):
if i == self.current_page:
tmp = '<li class="active"><a href="/{0}/?page={1}">{1}</a></li>'.format(self.url_prefix, i)
else:
tmp = '<li><a href="/{0}/?page={1}">{1}</a></li>'.format(self.url_prefix, i)
li_list.append(tmp)
# 添加下一頁
if self.current_page >= self.total_page: # 表示沒有下一頁
next_html = '<li class="disabled"><a aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'
else:
next_html = '<li><a href="/{}/?page={}" aria-label="Previous"><span aria-hidden="true">&raquo;</span></a></li>'.format(
self.url_prefix, self.current_page + 1)
li_list.append(next_html)
# 添加尾頁
li_list.append('<li><a href="/{}/?page={}">尾頁</a></li>'.format(self.url_prefix, self.total_page))
# 添加結尾nav和ul標籤
li_list.append("""
</ul>
</nav>
""")
# 將生成的標籤拼接成一個大字符串
page_html = ''.join(li_list)
return page_html


導入文件後調用代碼:
# 獲取分頁所需數據
total_num = data.count() # 獲取數據總計條數
current_page = request.GET.get('page') # 獲取當前頁頁碼
url_prefix = request.path_info.strip('/') # 獲取a標籤所需參數
# 調用分頁類# 調用類傳入參數生成實例page_obj = mypage.Page(total_num, current_page, url_prefix, per_page=1)# 按照分頁所需數據對總數據進行分割獲取當前頁面所需展現的數據data = data[page_obj.data_start:page_obj.data_end]# 獲取HTML代碼page_html = page_obj.page_html()
相關文章
相關標籤/搜索