欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python自定義分頁(yè)器的實(shí)現(xiàn)

 更新時(shí)間:2022年04月13日 15:14:54   作者:Rickyyan  
這篇文章主要介紹了python自定義分頁(yè)器的實(shí)現(xiàn),通過自定義分頁(yè)器封裝展開主題并對(duì)其實(shí)用方法簡(jiǎn)單介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下

自定義分頁(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>

python自定義分頁(yè)器_python

到此這篇關(guān)于python自定義分頁(yè)器的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python自定義分頁(yè)器內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python用match()函數(shù)爬數(shù)據(jù)方法詳解

    python用match()函數(shù)爬數(shù)據(jù)方法詳解

    在本篇文章里小編給大家整理了關(guān)于python用match()函數(shù)爬數(shù)據(jù)方法以及相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。
    2019-07-07
  • Python多線程原理與用法實(shí)例剖析

    Python多線程原理與用法實(shí)例剖析

    這篇文章主要介紹了Python多線程原理與用法,結(jié)合具體的爬蟲實(shí)例剖析了多線程的相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下
    2019-01-01
  • python基礎(chǔ)學(xué)習(xí)之組織文件

    python基礎(chǔ)學(xué)習(xí)之組織文件

    今天帶大家復(fù)習(xí)python基礎(chǔ)知識(shí),此文章將要介紹如何組織文件,既拷貝,移動(dòng)等,文中有非常詳細(xì)的代碼示例,對(duì)正在學(xué)習(xí)python的小伙伴們很有幫助,需要的朋友可以參考下
    2021-05-05
  • Python圖像運(yùn)算之圖像閾值化處理詳解

    Python圖像運(yùn)算之圖像閾值化處理詳解

    這篇文章將詳細(xì)講解圖像閾值化處理,涉及閾值化處理、固定閾值化處理和自適應(yīng)閾值化處理,這是圖像邊緣檢測(cè)或圖像增強(qiáng)等處理的基礎(chǔ),感興趣的可以了解一下
    2022-04-04
  • 一文帶你探尋Python中的裝飾器

    一文帶你探尋Python中的裝飾器

    這篇文章就來和大家詳細(xì)講一講Python中裝飾器的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),對(duì)我們深入了解Python有一定的幫助,感興趣的可以了解一下
    2023-04-04
  • 用python寫個(gè)自動(dòng)SSH登錄遠(yuǎn)程服務(wù)器的小工具(實(shí)例)

    用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

    今天小編就為大家分享一篇使用pandas 將DataFrame轉(zhuǎn)化成dict,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 解決使用PyCharm時(shí)無法啟動(dòng)控制臺(tái)的問題

    解決使用PyCharm時(shí)無法啟動(dòng)控制臺(tái)的問題

    今天小編就為大家分享一篇解決使用PyCharm時(shí)無法啟動(dòng)控制臺(tái)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • python使用mailbox打印電子郵件的方法

    python使用mailbox打印電子郵件的方法

    這篇文章主要介紹了python使用mailbox打印電子郵件的方法,涉及Python打印電子郵件的相關(guān)技巧,需要的朋友可以參考下
    2015-04-04
  • Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文

    Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文

    今天小編就為大家分享一篇Django 實(shí)現(xiàn)xadmin后臺(tái)菜單改為中文,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11

最新評(píng)論