python 自定義分頁元件

2022-05-19 14:54:51 字數 3241 閱讀 2432

使用方法

"""

自定義分頁元件的使用方法:

pager_obj = pagination(request.get.get('page',1),len(host_list),request.path_info,request.get)

host_list = host_list[pager_obj.start:pager_obj.end]#每頁顯示的長度

html = pager_obj.page_html()#返回的頁碼

return render(request,'hosts.html',)

"""

class

pagination(object):

def__init__(self, current_page, totalcount, base_url,parmas, per_page_count=10, max_page_count=11):

try:

current_page =int(current_page)

except

: current_page = 1

if current_page < 1:

current_page = 1

#當前頁 self.current_page =current_page

#總記錄數/總條數

self.totalcount =totalcount

#每頁顯示的數量

self.per_page_count =per_page_count

max_page_num, v =divmod(totalcount, per_page_count)

ifv:

max_page_num += 1

#總頁數 self.max_page_num =max_page_num

#顯示頁碼的個數

#頁面上預設顯示11個頁面(當前頁在中間)

self.max_page_count =max_page_count

self.half_page_count = int((self.max_page_count - 1) / 2)

#url字首

self.base_url =base_url

import

copy

parmas=copy.deepcopy(parmas)

print(parmas,'

------******')

self.parmas=parmas

#每頁開始的記錄

@property

defstart(self):

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

#每頁結束的記錄

@property

defend(self):

return self.per_page_count *self.current_page

#頁碼顯示

defpage_html(self):

if self.max_page_num

page_start = 1page_end =self.max_page_num

else

:

if self.current_page

page_start = 1page_end =self.max_page_count

else

:

if (self.current_page + self.half_page_count) >self.max_page_num:

page_start = self.max_page_num - self.max_page_count + 1page_end =self.max_page_num

else

: page_start = self.current_page -self.half_page_count

page_end = self.current_page +self.half_page_count

html_list =

#首頁self.parmas['

page

'] = 1first_page = '

首頁' %(self.base_url,self.parmas.urlencode())

#if self.current_page == 1:

pre_page='«'

#else

: self.parmas[

'page

'] =self.current_page-1

#pre_page = '«

'%( self.base_url, self.parmas.urlencode())

for i in range(page_start, page_end + 1):

self.parmas[

'page

'] =i

if i ==self.current_page:

#tmp = "%s" % (self.base_url, self.parmas.urlencode(), i)

tmp='

%s'%(self.base_url, self.parmas.urlencode(), i)

else

: tmp='

%s'%(self.base_url, self.parmas.urlencode(), i)

#if self.current_page ==self.max_page_num:

# next_page='»'

else

: self.parmas[

'page

']=self.current_page+1

#next_page= next_page='

»'%(self.base_url,self.parmas.urlencode())

self.parmas[

'page

']=self.max_page_num

last_url = "

尾頁" %(self.base_url, self.parmas.urlencode())

return

''.join(html_list)

pager

django 自定義分頁元件

分頁元件應用 在檢視函式中 queryset models.issues.objects.filter project id project id page object pagination current page request.get.get page all count queryset....

Python 自定義分頁

django內建了分頁設定,但不夠靈活,需要我們自定義分頁。下面 考慮了利用bootstrap分頁元件來實現頁面美化,如果不想使用,可以不呼叫url format by bootstrap函式,即可實現普通分頁。具體的引數和 均有注釋 python3 project mysite2 pager.py...

Python 自定義分頁

使用流程 分頁元件應用 1.在檢視函式中 queryset models.issues.objects.filter project id project id page object pagination current page request.get.get page all count qu...