python web框架中實(shí)現(xiàn)原生分頁
更新時間:2019年09月08日 14:45:20 作者:wxp_2001
這篇文章主要為大家詳細(xì)介紹了python web框架中使用原生分頁的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python web框架實(shí)現(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: 當(dāng)前頁碼
:param all_count: 數(shù)據(jù)總條數(shù)
:param pre: 每頁數(shù)據(jù)量
:param max_show: 最多頁碼數(shù)
'''
try:
self.p = int(p) # 傳進(jìn)來的頁碼
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python+selenium對table表和分頁處理
- Python優(yōu)化列表接口進(jìn)行分頁示例實(shí)現(xiàn)
- python中Tkinter實(shí)現(xiàn)分頁標(biāo)簽的示例代碼
- 利用python對mysql表做全局模糊搜索并分頁實(shí)例
- python Django框架實(shí)現(xiàn)web端分頁呈現(xiàn)數(shù)據(jù)
- Python Django實(shí)現(xiàn)layui風(fēng)格+django分頁功能的例子
- python 實(shí)現(xiàn)分頁顯示從es中獲取的數(shù)據(jù)方法
- python自定義分頁器的實(shí)現(xiàn)
相關(guān)文章
Python實(shí)現(xiàn)感知機(jī)(PLA)算法
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)感知機(jī)(PLA)算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
python爬蟲把url鏈接編碼成gbk2312格式過程解析
這篇文章主要介紹了python爬蟲把url鏈接編碼成gbk2312格式過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06
Python django框架 web端視頻加密的實(shí)例詳解
這篇文章主要介紹了Python django框架 web端視頻加密,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
利用Python對中國500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析
這篇文章主要介紹了利用Python對中國500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析,從不同角度去對數(shù)據(jù)進(jìn)行統(tǒng)計(jì)分析,可視化展示,下文詳細(xì)內(nèi)容介紹需要的小伙伴可以參考一下2022-05-05
Django集成富文本編輯器summernote的實(shí)現(xiàn)步驟
在最近的項(xiàng)目中小編使用了這個富文本編輯器,選擇它的主要原因是配置非常簡單,默認(rèn)支持普通用戶上傳圖片(不像ckeditor默認(rèn)只有staff user才能上傳圖片。如果要讓普通用戶上傳圖片,還需修改源碼裝飾器)?,F(xiàn)在讓我們來看看如何使用這個富文本編輯器2021-05-05
Pytest參數(shù)化parametrize使用代碼實(shí)例
這篇文章主要介紹了Pytest參數(shù)化parametrize使用代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02

