python web框架中實現(xiàn)原生分頁
更新時間:2019年09月08日 14:45:20 作者:wxp_2001
這篇文章主要為大家詳細介紹了python web框架中使用原生分頁的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python web框架實現(xiàn)原生分頁的具體代碼,供大家參考,具體內(nèi)容如下
原生分頁器 示例
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Pagination:
def __init__(self, p, all_count, pre=10, max_show=11):
'''
:param p: 當前頁碼
:param all_count: 數(shù)據(jù)總條數(shù)
:param pre: 每頁數(shù)據(jù)量
:param max_show: 最多頁碼數(shù)
'''
try:
self.p = int(p) # 傳進來的頁碼
if self.p <= 0:
self.p = 1
except Exception as e:
self.p = 1
# 總量
# all_count = all_count
# pre = per # 每頁數(shù)據(jù)條數(shù)
total_num, more = divmod(all_count, pre)
if more:
total_num += 1 # total_num總數(shù)據(jù)頁數(shù)
# 顯示頁碼數(shù)
max_show = max_show
if total_num <= max_show: # 總數(shù)據(jù)量很小
page_start = 1
page_end = total_num
else:
if self.p - max_show // 2 <= 0: # 防止左邊出現(xiàn)0頁
page_start = 1
page_end = max_show
elif self.p + max_show // 2 >= total_num + 1: # 防止右邊出現(xiàn)超出
page_end = total_num
page_start = page_end - max_show
else:
page_start = self.p - max_show // 2
page_end = self.p + max_show // 2
# 數(shù)據(jù)的起始結(jié)束
self.start = (self.p - 1) * pre
self.end = self.p * pre
# 頁碼
self.page_start = page_start
self.page_end = page_end
self.total_num = total_num
@property
def page_html(self):
li_list = []
for i in range(self.page_start, self.page_end + 1):
if i == self.p:
li_list.append('<li class="active"><a href="?p={}" >{}</a></li>'.format(i, i))
else:
li_list.append('<li><a href="?p={}" >{}</a></li>'.format(i, i))
# 添加頁首 頁尾
li_list.insert(0,
'<li><a href="?p={}" aria-label="Previous"><span aria-hidden="true">«</span></a></li>'.format(
self.p - 1))
li_list.append(
'<li><a href="?p={}" aria-label="Next"><span aria-hidden="true">»</span> </a></li>'.format(self.p + 1))
if self.p == 1:
li_list[0] = '<li class="disabled"><span aria-hidden="true">«</span></li>'
elif self.p == self.total_num:
li_list[-1] = '<li class="disabled"><span aria-hidden="true">»</span></li>'
pagehtml = ''.join(li_list)
return pagehtml
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
python將txt文檔每行內(nèi)容循環(huán)插入數(shù)據(jù)庫的方法
今天小編就為大家分享一篇python將txt文檔每行內(nèi)容循環(huán)插入數(shù)據(jù)庫的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python tkinter實現(xiàn)簡單加法計算器代碼實例
這篇文章主要介紹了Python tkinter實現(xiàn)簡單加法計算器代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05
Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)
這篇文章主要介紹了Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-03-03

