自定義分頁器

2022-07-25 07:45:13 字數 3480 閱讀 5221

簡單直接,上**!!

在專案的根目錄下新建乙個包(帶有__init__.py檔案的資料夾),或者直接將下面的pagenation包複製到你的專案中

新建乙個py檔案,如:pagenation.py

匯入元件 mark_safe

自定義類

''':param request: request物件

:param all_count: 總資料量

:param base_url: 路徑

:param max_show: 最多顯示的頁碼數

:param per_num: 每頁顯示的資料條數

'''#

基本的url

self.base_url =request.path_info

#當前頁碼

try:

self.current_page = int(request.get.get('

page

', 1))

if self.current_page <=0:

self.current_page = 1

except

exception as e:

self.current_page = 1

#最多顯示的頁碼數

self.max_show =max_show

half_show = max_show // 2

#每頁顯示的資料條數

self.per_num =per_num

#總資料量

self.all_count =all_count

#總頁碼數

self.total_num, more =divmod(all_count, per_num)

ifmore:

self.total_num += 1

#總頁碼數小於最大顯示數,顯示總頁碼數

if self.total_num <=max_show:

self.page_start = 1self.page_end =self.total_num

else

:

#總頁碼數大於最大顯示數,最多顯示11個

if self.current_page <=half_show:

self.page_start = 1self.page_end =max_show

elif self.current_page + half_show >=self.total_num:

self.page_end =self.total_num

self.page_start = self.total_num - max_show + 1

else

: self.page_start = self.current_page -half_show

self.page_end = self.current_page +half_show

@property

defstart(self):

''':return: start page

'''return (self.current_page - 1) *self.per_num

@property

defend(self):

''':return:end page

'''return self.current_page *self.per_num

@property

defshow_li(self):

#存放 li **

html_list =

if self.current_page == 1:

first_li = '首頁'

else

: first_li = '首頁'

.format(self.base_url)

if self.current_page == 1:

prev_li = '

<<

'else

: prev_li = '

<<

'.format(self.current_page - 1,

self.base_url)

for num in range(self.page_start, self.page_end + 1):

if self.current_page ==num:

li_html = '

'.format(num, self.base_url)

else

: li_html = '

'.format(num, self.base_url)

if self.current_page ==self.total_num:

next_li = '

>>

'else

: next_li = '

>>

'.format(self.current_page + 1,

self.base_url)

if self.current_page ==self.total_num:

last_li = '末頁'

else

: last_li = '末頁'

.format(self.total_num, self.base_url)

return mark_safe(''.join(html_list))

pagenation.py

使用時,在你的py檔案(如:views.py)中匯入該檔案

from pagenation.pagenation import pagenation

def index(request):

# 資料

book_list = models.book.objects.all()

# 分頁

page = pagenation(request, book_list.count())

return render(request, 'index.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...

自定義分頁器

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

Swiper自定義分頁器

rel stylesheet href path to swiper.min.css class swiper container class swiper slide slide 1div class swiper slide slide 2div class swiper slide slide...