python web框架中實(shí)現(xiàn)原生分頁(yè)
本文實(shí)例為大家分享了python web框架實(shí)現(xiàn)原生分頁(yè)的具體代碼,供大家參考,具體內(nèi)容如下
原生分頁(yè)器 示例
#!/usr/bin/env python
# -*- coding:utf-8 -*-
class Pagination:
def __init__(self, p, all_count, pre=10, max_show=11):
'''
:param p: 當(dāng)前頁(yè)碼
:param all_count: 數(shù)據(jù)總條數(shù)
:param pre: 每頁(yè)數(shù)據(jù)量
:param max_show: 最多頁(yè)碼數(shù)
'''
try:
self.p = int(p) # 傳進(jìn)來(lái)的頁(yè)碼
if self.p <= 0:
self.p = 1
except Exception as e:
self.p = 1
# 總量
# all_count = all_count
# pre = per # 每頁(yè)數(shù)據(jù)條數(shù)
total_num, more = divmod(all_count, pre)
if more:
total_num += 1 # total_num總數(shù)據(jù)頁(yè)數(shù)
# 顯示頁(yè)碼數(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頁(yè)
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
# 頁(yè)碼
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))
# 添加頁(yè)首 頁(yè)尾
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python+selenium對(duì)table表和分頁(yè)處理
- Python優(yōu)化列表接口進(jìn)行分頁(yè)示例實(shí)現(xiàn)
- python中Tkinter實(shí)現(xiàn)分頁(yè)標(biāo)簽的示例代碼
- 利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例
- python Django框架實(shí)現(xiàn)web端分頁(yè)呈現(xiàn)數(shù)據(jù)
- Python Django實(shí)現(xiàn)layui風(fēng)格+django分頁(yè)功能的例子
- python 實(shí)現(xiàn)分頁(yè)顯示從es中獲取的數(shù)據(jù)方法
- python自定義分頁(yè)器的實(shí)現(xiàn)
相關(guān)文章
python將txt文檔每行內(nèi)容循環(huán)插入數(shù)據(jù)庫(kù)的方法
今天小編就為大家分享一篇python將txt文檔每行內(nèi)容循環(huán)插入數(shù)據(jù)庫(kù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python tkinter實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器代碼實(shí)例
這篇文章主要介紹了Python tkinter實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù)
這篇文章主要介紹了Python OpenCV 基于圖像邊緣提取的輪廓發(fā)現(xiàn)函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03
從CentOS安裝完成到生成詞云python的實(shí)例
下面小編就為大家分享一篇從CentOS安裝完成到生成詞云python的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-12-12
理解python多線程(python多線程簡(jiǎn)明教程)
這篇文章主要介紹了理解python多線程,一個(gè)快速理解python多線程的簡(jiǎn)明教程,需要的朋友可以參考下2014-06-06
Python面向?qū)ο笾^承和組合用法實(shí)例分析
這篇文章主要介紹了Python面向?qū)ο笾^承和組合用法,結(jié)合實(shí)例形式分析了Python面向?qū)ο蟪绦蛟O(shè)計(jì)中組合與繼承的相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2018-08-08
Python 實(shí)時(shí)獲取任務(wù)請(qǐng)求對(duì)應(yīng)的Nginx日志的方法
本文給大家分享Python 實(shí)時(shí)獲取任務(wù)請(qǐng)求對(duì)應(yīng)的Nginx日志的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-07-07

