django 自定義分頁元件

2022-06-20 03:21:13 字數 3516 閱讀 3130

"""

分頁元件應用:

在檢視函式中

queryset = models.issues.objects.filter(project_id=project_id)

page_object = pagination(

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

all_count=queryset.count(),

base_url=request.path_info,

query_params=request.get

)issues_object_list = queryset[page_object.start:page_object.end]

context =

return render(request, 'issues.html', context)前端}

class pagination(object):

definit(self, current_page, all_count, base_url, query_params, per_page=30, pager_page_count=11):

"""分頁初始化

:param current_page: 當前頁碼

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

:param all_count: 資料庫中總條數

:param base_url: 基礎url

:param query_params: querydict物件,內部含所有當前url的原條件

:param pager_page_count: 頁面上最多顯示的頁碼數量

"""self.base_url = base_url

try:

self.current_page = int(current_page)

if self.current_page <= 0:

self.current_page = 1

except exception as e:

self.current_page = 1

query_params = query_params.copy()

query_params._mutable = true

self.query_params = query_params

self.per_page = per_page

self.all_count = all_count

self.pager_page_count = pager_page_count

pager_count, b = divmod(all_count, per_page)

if b != 0:

pager_count += 1

self.pager_count = pager_count

half_pager_page_count = int(pager_page_count / 2)

self.half_pager_page_count = half_pager_page_count

@property

def start(self):

"""資料獲取值起始索引

:return:

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

@property

def end(self):

"""資料獲取值結束索引

:return:

"""return self.current_page * self.per_page

def page_html(self):

"""生成html頁碼

:return:

"""if self.all_count == 0:

return ""

# 如果資料總頁碼pager_count<11 pager_page_count

if self.pager_count < self.pager_page_count:

pager_start = 1

pager_end = self.pager_count

else:

# 資料頁碼已經超過11

# 判斷: 如果當前頁 <= 5 half_pager_page_count

if self.current_page <= self.half_pager_page_count:

pager_start = 1

pager_end = self.pager_page_count

else:

# 如果: 當前頁+5 > 總頁碼

if (self.current_page + self.half_pager_page_count) > self.pager_count:

pager_end = self.pager_count

pager_start = self.pager_count - self.pager_page_count + 1

else:

pager_start = self.current_page - self.half_pager_page_count

pager_end = self.current_page + self.half_pager_page_count

page_list =

if self.current_page <= 1:

' else:

self.query_params['page'] = self.current_page - 1

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

for i in range(pager_start, pager_end + 1):

self.query_params['page'] = i

if self.current_page == i:

tpl = '%s

' % (

self.base_url, self.query_params.urlencode(), i,)

else:

tpl = '%s

' % (self.base_url, self.query_params.urlencode(), i,)

if self.current_page >= self.pager_count:

' else:

self.query_params['page'] = self.current_page + 1

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

if self.all_count:

tpl = "共%s條資料,頁碼%s/%s頁

" % (

self.all_count, self.current_page, self.pager_count,)

page_str = "".join(page_list)

return page_str

Django之自定義分頁元件

from django.utils.safestring import mark safe mark safe 安全字串 class mypage def init self,page num,page num count,req path,per page num,page num show se...

django 自定義分頁

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

django自定義分頁

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