您好,登錄后才能下訂單哦!
class Page():
def __init__(self, page_num, total_count, base_url, params, per_page=10, max_page=11):
"""
page_num: 當前頁碼數
total_count: 數據總數
base_url: a標簽href的前綴
params: request對象,是QueryDict類型
per_page: 每頁顯示多少條數據
max_page: 單一頁面上最多顯示幾個頁碼
"""
self.page_num = page_num
self.total_count = total_count
self.base_url = base_url
self.params = params
self.per_page = per_page
self.max_page = max_page
# 總頁碼
total_page, m = divmod(self.total_count, self.per_page)
if m:
total_page += 1
self.total_page = total_page
try:
self.page_num = int(self.page_num)
# print("self.page_num==>", self.page_num) 1
# 如果輸入的頁碼數大于最大的頁碼數,默認返回第一頁
if self.page_num > self.total_page:
self.page_num = self.total_page
elif self.page_num <= 0:
self.page_num = 1
except Exception as e:
# 當輸入的頁碼不是數字時,返回第一頁
self.page_num = 1
#如果當前頁碼數為0,那么數據開始位置也是0
if self.page_num == 0:
self.data_start = self.page_num
# 定義兩個變量保存數據從哪取到哪
else:
self.data_start = (self.page_num - 1) * 10
self.data_end = self.page_num * 10
#頁面上總共展示多少頁碼
#如果總頁數小于設定的單頁顯示頁碼數
if self.total_page < self.max_page:
self.max_page = self.total_page
# 讓選定頁碼取中,計算左右兩邊長度
self.half_max_page = self.max_page // 2
# 頁面上展示的頁碼從哪開始
self.page_start = self.page_num - self.half_max_page
# 頁面上展示的頁碼從哪結束
self.page_end = self.page_num + self.half_max_page
# 如果開始頁數小于1
if self.page_start <= 1:
self.page_start = 1
self.page_end = self.max_page
# 如果結束頁面大于最大頁碼數
elif self.page_end >= self.total_page:
self.page_start = self.total_page - self.max_page + 1
self.page_end = self.total_page
#拷貝一份request對象,是個QueryDict形式,后面會用于獲取及修改url
import copy
self.params = copy.deepcopy(self.params) #{"page":"12","title_startwith":"py","id_gt":"5"}
@property
def start(self):
return self.data_start
@property
def end(self):
return self.data_end
def page_html(self):
# 自己拼接分頁的HTML代碼
html_str_list = []
print("html_str_list==>", html_str_list)
# 加上上一頁標簽
#判斷一下如果是第一頁就沒有上一頁了
if self.page_num <= 1:
html_str_list.append('<li class="disabled"><a href="#"><span aria-hidden="true">?</span></a></li>')
#否則加上上一頁
else:
self.params["page"] = self.page_num - 1
html_str_list.append('<li><a href="{0}?{1}"><span aria-hidden="true">?</span></a></li>'.format(self.base_url ,self.params.urlencode()))
# 加上首頁
self.params["page"] = 1
html_str_list.append('<li><a href="{0}?{1}">首頁</a></li>'.format(self.base_url, self.params.urlencode()))
for i in range(self.page_start, self.page_end + 1):
#把QueryDict中的page修改為當前循環的頁碼
self.params["page"] = i
# 如果i等于當前頁就加一個樣式類
if i == self.page_num:
tmp = '<li class="active"><a href="{0}?page={1}">{2}</a></li>'.format(self.base_url, self.params.urlencode(), i)
else:
tmp = '<li><a href="{0}?{1}">{2}</a></li>'.format(self.base_url, self.params.urlencode(), i)
html_str_list.append(tmp)
# 加上尾頁
self.params["page"] = self.total_page
html_str_list.append('<li><a href="{0}?{1}">尾頁</a></li>'.format(self.base_url, self.params.urlencode()))
# 加上下一頁標簽
#判斷,如果是最后一頁,就沒有下一頁
if self.page_num >= self.total_page:
html_str_list.append('<li class="disabled"><a href="#"><span aria-hidden="true">?</span></a></li>')
else:
self.params["page"] = self.page_num + 1
html_str_list.append('<li><a href="{0}?{1}"><span aria-hidden="true">?</span></a></li>'.format(self.base_url, self.params.urlencode()))
page_html = "".join(html_str_list)
return page_html
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。