django自定義分頁

2022-03-28 01:57:45 字數 3248 閱讀 7462

django框架雖然帶有自己的分頁功能,但是樣式不好看,為了做更好看的分頁,需要自定義乙個分頁工具,話不多說擼起袖子就開幹。

1.首先先建乙個python檔案叫pager.py**如下:

class pagination(object):

page_url 翻頁url路由

object_list 資料來源列表

current_page 當前頁

page_size 每頁顯示的資料條數

maxpagenum 分頁條顯示的分頁數

def __init__(self, page_url, object_list, current_page, page_size=10, maxpagenum=7):

# 翻頁的url

self.page_url = page_url

# 分資料來源

self.object_list = object_list

# 資料總個數

self.total_count = object_list.count()

# 當前頁

try:

v =int(current_page)

if v <=0:

v =1

self.current_page = v

except exception as e:

self.current_page =1

# 每頁顯示的行數

self.per_page_item_num = page_size

# 最多顯示頁面

self.max_page_num = maxpagenum

def start(self):

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

def end(self):

return self.current_page *self.per_page_item_num

# 獲取分頁後的資料

def page_list(self):

return self.object_list[self.start():self.end()]

@property

def num_pages(self):

總頁數:return:

a, b =divmod(self.total_count, self.per_page_item_num)

if b ==0:

return a

return a +1

def pager_num_range(self):

# self.num_pages()

# self.num_pages

# 當前頁

# current_page

# 最多顯示的頁碼數量 11

# self.per_pager_num

# 總頁數

# self.num_pages

if self.num_pages

return range(1, self.num_pages +1)

# 總頁數特別多 5

part =int(self.max_page_num /2)

if self.current_page <= part:

return range(1, self.max_page_num +1)

if (self.current_page + part) >self.num_pages:

return range(self.num_pages -self.max_page_num +1, self.num_pages +1)

return range(self.current_page - part, self.current_page + part +1)

def page_str(self):

page_list =

first ="" %self.page_url

if self.current_page ==1:

"else:

prev ="

" % (self.page_url, self.current_page -1,)

for iin self.pager_num_range():

if i ==self.current_page:

temp ="%s" % (self.page_url, i, i)

else:

temp ="%s

" % (self.page_url, i, i)

if self.current_page ==self.num_pages:

"else:

nex ="%s

" % (self.page_url, self.current_page +1,)

last ="尾頁

" % (self.page_url, self.num_pages,)

return ''.join(page_list)

2.那麼建好了這個自定義的分頁器後,就開始來測試看看效果怎麼樣了:

在templates裡新建page.html,views檔案裡加如乙個新的函式比如叫page:(注意要記的在urls裡新增page的路由)

2.1 views裡的**: 

# 使用自定義分頁

def page(request):

# 引入剛剛定義好的分頁工具

from example.pager import pagination

# 獲取翻頁的頁碼

current_page = request.get.get('page')

# 查詢資料

articles = article.objects.all()

# 引數說明 第乙個為分頁的url 第二個為分頁資料來源,第三個為當前頁,第四個為每頁顯示的資料條數,第五個為分頁條分組預設為7個一組

pager = pagination('/page/', articles, current_page, 5, 7)

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

2.2 page.html **:

自定義分頁

2.3 base.html 其實就是引入bootstrap :

3.執行效果:

4.總結:

好了,django自定義分頁就到這裡了,但是,這種分頁是有些問題存在的,從**裡可以到是通過先查全部資料出來然後對資料列表進行分頁處理,當資料量大的時候就不建議這樣做了,那麼如何做高效的進行分頁呢,下篇文章將會做優化,喜歡記得關注我哦,當然,有問題也歡迎提出來!

django 自定義分頁

django框架雖然帶有自己的分頁功能,但是樣式不好看,為了做更好看的分頁,需要自定義乙個分頁工具,話不多說擼起袖子就開幹。1.首先先建乙個python檔案叫pager.py 如下 class pagination object page url 翻頁url路由 object list 資料來源列表...

django 自定義分頁功能

django自帶的分頁功能有侷限性,只能顯示所有頁碼,不能顯示當前頁的前五和後五頁的頁碼。所有需要自己寫。from django.shortcuts import render class mypaginator def init self,cur page,per page,total,show ...

django 自定義分頁元件

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