自定義分頁器的使用

2022-07-21 23:39:20 字數 3791 閱讀 4385

01、page.py

#

from ..models import article, articleupdown, article2tag, comment, category, userinfo, blog, tag

import

copy

class

pagination(object):

def__init__(self, current_page, all_count, request, per_page=10, max_pager_num=11):

""":param current_page:當前頁

:param all_count: 所有資料總數

:param request:

:param per_page: 每一頁顯示的資料條數

:param max_pager_num: 這個頁面最多顯示的頁碼數量

num_pages:總資料所分頁的總頁面數

"""#

減少小於的時候,讓其展示頁面1的資料

try:

current_page =int(current_page)

except

exception as e:

current_page = 1

if current_page < 1:

current_page = 1self.current_page =current_page

self.all_count =all_count

self.per_page = per_page #

每一頁顯示的資料條數

#計算總頁數

print("計算"

,all_count,per_page)

num_pages, tmp = divmod(all_count, per_page) #

多的一條資料也展示一頁

print

(num_pages,tmp)

iftmp:

num_pages += 1self.num_pages =num_pages

self.max_pager_num = max_pager_num #

最大顯示的頁數

self.page_count_half = int((self.max_pager_num - 1) / 2) #

最多顯示頁數的一半

self.params =copy.deepcopy(request.get)

print("

urlencode

", self.params.urlencode())

@property

defstart(self):

return (self.current_page - 1) *self.per_page

@property

defend(self):

return self.current_page *self.per_page

defpage_html(self):

#如果總頁數小於11

if self.num_pages <=self.max_pager_num:

pager_start = 1pager_end = self.num_pages + 1

else

:

if self.current_page <=self.page_count_half:

pager_start = 1pager_end = self.max_pager_num + 1

#當前頁大於5

elif (self.current_page + self.page_count_half) >self.num_pages:

pager_start = self.num_pages - self.max_pager_num + 1pager_end = self.num_pages + 1

else

: pager_start = self.current_page -self.page_count_half

pager_end = self.current_page + self.page_count_half + 1page_html_list =

# self.params["

page

"] = 1first_page = '

' %(self.params.urlencode())

print("

*****==

", ''

.join(page_html_list))

return

''.join(page_html_list)

02.views.py

from .page import

pagination

defpag01(request):

print

(request.get)

#總數all_count =book.objects.all()

current_page = request.get.get("

page")

print("

2>>

", current_page)

pagination = pagination(current_page, all_count.count(), request, per_page=2)

print("

---"

, pagination.page_html)

#當前頁面的所有資料的顯示範圍

current_page_obj =book.objects.all()[pagination.start:pagination.end]

print("

當前頁面的所有資料〉〉

",current_page_obj)

return render(request, "index.html

", locals())

03、index.html

doctype html

>

<

html

lang

="en"

>

<

head

>

<

meta

charset

="utf-8"

>

<

title

>自定義分頁器

title

>

<

link

rel="stylesheet"

href

=""integrity

="sha384-bvyiisifek1dgmjrakycuhahrg32omucww7on3rydg4va+pmstsz/k68vbdejh4u"

crossorigin

="anonymous"

>

head

>

<

h1>自定義分頁器

h1>

<

body

>

<

div>

<

ul>

<

li>}:}

li>

ul>

div>

<

div

class

="page_region pull-right"

>

}div

>

body

>

html

>

antd自定義分頁器 自定義分頁器例項

def init self,current page num,all count,request,per page num 2,pager count 11 封裝分頁相關資料 param current page num 當前訪問頁的數字 param all count 分頁資料中的資料總條數 pa...

自定義分頁器

簡單直接,上 在專案的根目錄下新建乙個包 帶有 init py檔案的資料夾 或者直接將下面的pagenation包複製到你的專案中 新建乙個py檔案,如 pagenation.py 匯入元件 mark safe 自定義類 param request request物件 param all count...

自定義分頁器

class pagination object def init self,current page,all count,per page num 2,pager count 11 封裝分頁相關資料 param current page 當前頁 param all count 資料庫中的資料總條數 ...