导入: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
对象方法不相同
paginator 对象方法:
par_page: 每页显示条目数量
cout: 数据总个数
num_pages: 总页数
page_range: 总页数的索引范围(1,100)
page: page对象
page对象方法:
has_next 是否有下一页
next_page_number 下一页页码
has_previous 是否有上一页
object_list 分页之后的数据列表
number 当number前页
paginator paginator对象
————————————————————————————————————————————————————
class CustomPaginator(Paginator):
def __init__(self,current_page, per_pager_num,*args,**kwargs):
# 当前页
self.current_page = int(current_page)
# 最多显示的页码数量 11
self.per_pager_num = int(per_pager_num)
super(CustomPaginator,self).__init__(*args,**kwargs)
def pager_num_range(self):
# 当前页
#self.current_page
# 最多显示的页码数量 11
#self.per_pager_num
# 总页数
# self.num_pages
if self.num_pages < self.per_pager_num:
return range(1,self.num_pages+1)
# 总页数特别多 5
part = int(self.per_pager_num/2)
if self.current_page <= part:
return range(1,self.per_pager_num+1)
if (self.current_page + part) > self.num_pages:
return range(self.num_pages-self.per_pager_num+1,self.num_pages+1)
return range(self.current_page-part,self.current_page+part+1)
def index1(request):
current_page = request.GET.get('p')
# Paginator对象
paginator = CustomPaginator(current_page,7,USER_LIST, 10)
try:
posts = paginator.page(current_page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request,'index1.html',{'posts':posts})
原版分页parger.html
{% if posts.has_previous %}
上一页
{% endif %}
{% if posts.has_next %}
下一页
{% endif %}
{% for i in posts.paginator.pager_num_range %}
{% if i == posts.number %}
{{ i }}
{% else %}
{{ i }}
{% endif %}
{% endfor %}
{{ posts.number }}
/ {{ posts.paginator.num_pages }}
{% for row in posts%}
{{ row.name }}
{{ row.age }}
{% endfor %}
{% include 'include/parger.html' %}
# -*- coding:utf-8 -*-
class Pagination(object):
def __init__(self, totalCount, currentPage,perPageItemNum=10, maxPageNum=7):
# 数据总个数
self.total_count = totalCount
# 当前页
try:
v = int(currentPage)
if v <= 0:
v = 1
self.current_page = v
except Exception as e:
self.current_page = 1
# 每页显示的行数
self.per_page_item = perPageItemNum
# 每页最多显示页码数
self.max_page_num = maxPageNum
def start(self):
return (self.current_page-1) * self.per_page_item
def end(self):
return self.current_page * self.per_page_item
@property
def num_pages(self):
# 判断极值
# 总页数199 每页显示
a,b = divmod(self.total_count,self.per_page_item)
if b == 0:
return a
return a+1
# 要显示的页码
def pager_num_range(self):
# 当前页
# self.current_page
# 最多显示的页码数量 11
# self.per_pager_num
# 总页数
# self.num_pages
if self.num_pages < self.max_page_num:
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="首页 "
page_list.append(first)
if self.current_page == 1:
prev = "上一页 "
else:
prev = "上一页 " % (self.current_page - 1,)
page_list.append(prev)
for i in self.pager_num_range():
if i == self.current_page:
temp = "%s " % (i, i)
else:
temp = "%s " % (i, i)
page_list.append(temp)
if self.current_page == self.num_pages:
nex = "下一页 "
else:
nex = "下一页 " % (self.current_page + 1,)
page_list.append(nex)
last = "尾页 " % (self.num_pages,)
page_list.append(last)
return "".join(page_list)
def index2(request):
# 导入自定义py文件
from app01.pager import Pagination
# 获取当前页
current_page = request.GET.get('p')
# 创建对象传入值 199总数
page_obj = Pagination(199,current_page)
# 切片传入值
data_list = USER_LIST[page_obj.start():page_obj.end()]
return render(request,'index2.html',{"data":data_list,'page_obj':page_obj})
{% for row in data%}
{{ row.name }}
{{ row.age }}
{% endfor %}
{% for i in page_obj.pager_num_range %}
{{ i }}
{% endfor %}
{{ page_obj.page_str|safe }}