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

python自定義分頁器的實現(xiàn)

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

自定義分頁器封裝代碼

封裝分頁相關數(shù)據(jù):

  • :param current_page: 當前頁
  • :param all_count: 數(shù)據(jù)庫中的數(shù)據(jù)總條數(shù)
  • :param per_page_num: 每頁顯示的數(shù)據(jù)條數(shù)
  • :param pager_count: 最多顯示的頁碼個數(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

# 總頁碼
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):
# 如果總頁碼 < 11個:
if self.all_pager <= self.pager_count:
pager_start = 1
pager_end = self.all_pager + 1
# 總頁碼 > 11
else:
# 當前頁如果<=頁面上最多顯示11/2個頁碼
if self.current_page <= self.pager_count_half:
pager_start = 1
pager_end = self.pager_count + 1

# 當前頁大于5
else:
# 頁碼翻到最后
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標簽
page_html_list.append('''
<nav aria-label='Page navigation' style="text-align: center;">
<ul class='pagination'>
''')
first_page = '<li><a href="?page=%s">首頁</a></li>' % (1)
page_html_list.append(first_page)

if self.current_page <= 1:
prev_page = '<li class="disabled"><a href="#">上一頁</a></li>'
else:
prev_page = '<li><a href="?page=%s">上一頁</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="#">下一頁</a></li>'
else:
next_page = '<li><a href="?page=%s">下一頁</a></li>' % (self.current_page + 1,)
page_html_list.append(next_page)

last_page = '<li><a href="?page=%s">尾頁</a></li>' % (self.all_pager,)
page_html_list.append(last_page)
# 尾部添加標簽
page_html_list.append('''
</nav>
</ul>
''')
return ''.join(page_html_list)

自定義分頁器使用

后端

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

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

相關文章

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

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

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

    Python多線程原理與用法實例剖析

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

    python基礎學習之組織文件

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

    Python圖像運算之圖像閾值化處理詳解

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

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

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

    用python寫個自動SSH登錄遠程服務器的小工具(實例)

    下面小編就為大家?guī)硪黄胮ython寫個自動SSH登錄遠程服務器的小工具(實例)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 使用pandas 將DataFrame轉(zhuǎn)化成dict

    使用pandas 將DataFrame轉(zhuǎn)化成dict

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

    解決使用PyCharm時無法啟動控制臺的問題

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

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

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

    Django 實現(xiàn)xadmin后臺菜單改為中文

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

最新評論