分頁顯示
步驟:獲取當(dāng)前頁---獲取總數(shù)據(jù)---獲取分的總頁數(shù)--每頁顯示的條數(shù)---構(gòu)建Paganitor對(duì)象匹舞,需要兩個(gè)參數(shù)(總數(shù)據(jù)驯击,每頁顯示的條數(shù))----通過context給前段傳數(shù)據(jù)
from django.core.paginator import Paginator
def departall(request):
#構(gòu)建分頁
#獲取當(dāng)前頁碼
pagenow=int(request.GET.get('pagenow',1))
#獲取總的數(shù)據(jù)
depart_list=depart.objects.all().order_by('pk')
#每頁顯示的條數(shù)
pageSize=2
#構(gòu)建Paganitor
# --------總共的數(shù)據(jù),以及每頁顯示的條數(shù)
paginator = Paginator(depart_list, pageSize)
# pagenow 代表當(dāng)前的頁數(shù)
page = paginator.page(pagenow)
print(page.object_list)
context={
'depart_list':page.object_list,
'page_range':paginator.page_range,
'pagenow':pagenow
}
return render(request=request,template_name='companyall/departall2.html',context=context)