原理
def index(request):
per_page_count = 10
current_page=request.GET.get('p')
# page=1 0,10 0-9
# page=2 10,20 10-19
current_page = int(current_page)
start = (current_page - 1) * per_page_count
end = current_page * per_page_count
data=USER_LIST[start:end]
return render(request,'index.html',{'user_list':data})
1、Django内置分页
def index1(request):
from django.core.paginator import Paginator,EmptyPage,PageNotAnInteger
# 全部数据:USER_LIST,=> 得出共有多少条数据
# per_page: 每页显示条目数量
# count: 数据总个数
# num_pages: 总页数
# page_range: 总页数的索引范围:如:(1,10),(1,200)
# page: page对象(是否有下一页;是否有上一页)
paginator = Paginator(USER_LIST, 10)
current_page = request.GET.get("p")
try:
# page对象
posts = paginator.page(current_page)
# han_next 是否有下一页
# next_page_number 下一页页码
# has_previous 是否有上一页
# previous_page_number 上一页页码
# object_list 分页之后的数据列表,已经切片好的数据
# number 当前页
# paginator paginator对象
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return render(request,'index1.html',{'posts':posts})
<body>
<ul>
{% for row in posts.object_list %}
<li>{{ row.name }}-{{ row.age }}</li>
{% endfor %}
</ul>
{% include 'include/pager.html' %}
</body>
将分页代码独立:在template下新建目录include,在include创建pager.html
{% if posts.has_previous %}
<a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a>
{% endif %}
{% if posts.has_next %}
<a href="/index1.html?p={{ posts.next_page_number }}">下一页</a>
{% endif %}
<span>
{{ posts.number }} / {{ posts.paginator.num_pages }}
</span>
2、扩展django的内置分页
class MyPaginator(Paginator):
def __init__(self,current_page,per_pager_num,*args,**kwargs):
# 当前页
self.current_page = int(current_page)
# 最多显示的页码数量
self.per_pager_num = int(per_pager_num)
super(MyPaginator,self).__init__(*args,**kwargs)
# 在页面中显示页码的范围
def pager_num_range(self):
if self.num_pages < self.per_pager_num:
return range(1,self.num_pages+1)
# 总页数特别多
part = int(self.per_pager_num/2)
if self.current_page <= part:
return range(1,self.per_pager_num+1)
if self.current_page >= self.num_pages - part:
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)
{% if posts.has_previous %}
<a href="/index1.html?p={{ posts.previous_page_number }}">上一页</a>
{% endif %}
// 显示页码数字,提供跳转
{% for i in posts.paginator.pager_num_range %}
{% if i == posts.number %}
<a href="/index1.html?p={{ i }}" style="font-size: 30px;">{{ i }}</a>
{% else %}
<a href="/index1.html?p={{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% if posts.has_next %}
<a href="/index1.html?p={{ posts.next_page_number }}">下一页</a>
{% endif %}
<span>
{{ posts.number }} / {{ posts.paginator.num_pages }}
</span>
3、自定义分页
传入:所有数据的个数、当前页、每页显示条数、最多页码个数
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_num = perPageItemNum
# 最多显示页码
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
# @property 将方法伪装成属性
@property
def num_pages(self):
"""
总页数
"""
"""divmod python函数 传入被除数和除数 返回 商和余数"""
a,b = divmod(self.total_count,self.per_page_item_num)
if b == 0:
return a
return a+1
def pager_num_range(self):
if self.num_pages < self.max_page_num:
return range(1,self.num_pages+1)
# 总页数特别多
part = int(self.max_page_num/2)
if self.current_page <= part:
return range(1,self.max_page_num+1)
if self.current_page >= self.num_pages - part:
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="<li><a href='/index2.html?p=1'>首页</a></li>"
page_list.append(first)
if self.current_page==1:
prev="<li><a href='#'>上一页</a></li>"
else:
prev = "<li><a href='/index2.html?p=%s'>上一页</a></li>"%(self.current_page-1)
page_list.append(prev)
for i in self.pager_num_range():
if i==self.current_page:
temp = "<li class='active'><a href='/index2.html?p=%s'>%s</a></li>" % (i, i)
else:
temp = "<li><a href='/index2.html?p=%s'>%s</a></li>"%(i,i)
page_list.append(temp)
if self.current_page==self.num_pages:
nex = "<li><a href='#'>下一页</a></li>"
else:
nex = "<li><a href='/index2.html?p=%s'>下一页</a>"%(self.current_page+1)
page_list.append(nex)
last="<li><a href='/index2.html?p=%s'>尾页</a></li>"%(self.num_pages)
page_list.append(last)
return ''.join(page_list)
<body>
<ul>
{% for row in data %}
<li>{{ row.name }}-{{ row.age }}</li>
{% endfor %}
</ul>
<ul class="pagination pagination-sm">
{{ page_obj.page_str|safe }}
</ul>
</body>
标签:二十二,分页,self,posts,pages,current,num,page
From: https://www.cnblogs.com/zhlforhe/p/18013629