python自定義分頁(yè)器的實(shí)現(xiàn)
自定義分頁(yè)器封裝代碼
封裝分頁(yè)相關(guān)數(shù)據(jù):
- :param current_page: 當(dāng)前頁(yè)
- :param all_count: 數(shù)據(jù)庫(kù)中的數(shù)據(jù)總條數(shù)
- :param per_page_num: 每頁(yè)顯示的數(shù)據(jù)條數(shù)
- :param pager_count: 最多顯示的頁(yè)碼個(gè)數(shù)
class Pagination(object):
def __init__(self, current_page, all_count, per_page_num=2, pager_count=11):
try:
current_page = int(current_page)
except Exception as e:
current_page = 1
if current_page < 1:
current_page = 1
self.current_page = current_page
self.all_count = all_count
self.per_page_num = per_page_num
# 總頁(yè)碼
all_pager, tmp = divmod(all_count, per_page_num)
if tmp:
all_pager += 1
self.all_pager = all_pager
self.pager_count = pager_count
self.pager_count_half = int((pager_count - 1) / 2)
@property
def start(self):
return (self.current_page - 1) * self.per_page_num
@property
def end(self):
return self.current_page * self.per_page_num
def page_html(self):
# 如果總頁(yè)碼 < 11個(gè):
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 總頁(yè)碼 > 11
else:
# 當(dāng)前頁(yè)如果<=頁(yè)面上最多顯示11/2個(gè)頁(yè)碼
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1
# 當(dāng)前頁(yè)大于5
else:
# 頁(yè)碼翻到最后
if (self.current_page + self.pager_count_half) > self.all_pager:
pager_end = self.all_pager + 1
pager_start = self.all_pager - self.pager_count + 1
else:
pager_start = self.current_page - self.pager_count_half
pager_end = self.current_page + self.pager_count_half + 1
page_html_list = []
# 添加前面的nav和ul標(biāo)簽
page_html_list.append('''
<nav aria-label='Page navigation' style="text-align: center;">
<ul class='pagination'>
''')
first_page = '<li><a href="?page=%s">首頁(yè)</a></li>' % (1)
page_html_list.append(first_page)
if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一頁(yè)</a></li>'
else:
prev_page = '<li><a href="?page=%s">上一頁(yè)</a></li>' % (self.current_page - 1,)
page_html_list.append(prev_page)
for i in range(pager_start, pager_end):
if i == self.current_page:
temp = '<li class="active"><a href="?page=%s">%s</a></li>' % (i, i,)
else:
temp = '<li><a href="?page=%s">%s</a></li>' % (i, i,)
page_html_list.append(temp)
if self.current_page >= self.all_pager:
next_page = '<li class="disabled"><a href="#">下一頁(yè)</a></li>'
else:
next_page = '<li><a href="?page=%s">下一頁(yè)</a></li>' % (self.current_page + 1,)
page_html_list.append(next_page)
last_page = '<li><a href="?page=%s">尾頁(yè)</a></li>' % (self.all_pager,)
page_html_list.append(last_page)
# 尾部添加標(biāo)簽
page_html_list.append('''
</nav>
</ul>
''')
return ''.join(page_html_list)自定義分頁(yè)器使用
后端
from utils.mypage import Pagination
def get_book(request):
book_list = models.Book.objects.all()
current_page = request.GET.get("page",1)
all_count = book_list.count()
page_obj = Pagination(current_page=current_page,all_count=all_count,per_page_num=10)
page_queryset = book_list[page_obj.start:page_obj.end]
return render(request,'booklist.html',locals())前端
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
{% for book in page_queryset %}
<p>{{ book.title }}</p>
{% endfor %}
{{ page_obj.page_html|safe }}
</div>
</div>
</div>
到此這篇關(guān)于python自定義分頁(yè)器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python自定義分頁(yè)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python 實(shí)現(xiàn)分頁(yè)顯示從es中獲取的數(shù)據(jù)方法
- Python Django實(shí)現(xiàn)layui風(fēng)格+django分頁(yè)功能的例子
- python web框架中實(shí)現(xiàn)原生分頁(yè)
- python Django框架實(shí)現(xiàn)web端分頁(yè)呈現(xiàn)數(shù)據(jù)
- 利用python對(duì)mysql表做全局模糊搜索并分頁(yè)實(shí)例
- python中Tkinter實(shí)現(xiàn)分頁(yè)標(biāo)簽的示例代碼
- Python優(yōu)化列表接口進(jìn)行分頁(yè)示例實(shí)現(xiàn)
- python+selenium對(duì)table表和分頁(yè)處理
相關(guān)文章
python用match()函數(shù)爬數(shù)據(jù)方法詳解
在本篇文章里小編給大家整理了關(guān)于python用match()函數(shù)爬數(shù)據(jù)方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-07-07
用python寫個(gè)自動(dòng)SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)
下面小編就為大家?guī)硪黄胮ython寫個(gè)自動(dòng)SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
使用pandas 將DataFrame轉(zhuǎn)化成dict
今天小編就為大家分享一篇使用pandas 將DataFrame轉(zhuǎn)化成dict,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-12-12
解決使用PyCharm時(shí)無法啟動(dòng)控制臺(tái)的問題
今天小編就為大家分享一篇解決使用PyCharm時(shí)無法啟動(dòng)控制臺(tái)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文
今天小編就為大家分享一篇Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11

