一、基础篇
image.png=
view模块处理
li = []
for i in range(101):
li.append(i)
def user_list(request):
#当page 关键字不存在时 默认1
current_page = request.GET.get('page',1)
current_page = int(current_page)
#页数减-1*10,切片的起始位置
start = (current_page-1)*10
# 页数减*10,切片的结束位置
end = (current_page)*10
data = li[start:end]
#显示页数,因为页面没有循环数字的方法,所以后台处理
page_num,mod_num = divmod(len(li),10)
#余数不为0页数加一
if mod_num:
page_num += 1
temp_list = []
for i in range(1,page_num+1):
if i==current_page:
temp = '<a class="page active" href="/tp5/?page=%s">%s</a>'%(i,i)
else:
temp = '<a class="page" href="/tp5/?page=%s">%s</a>'%(i,i)
temp_list.append(temp)
temp_num = ''.join(temp_list)
return render(request,'user_list.html',{'num':temp_num,'data':data})
html 页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.pageination .page{
display: inline-block;
padding: 5px;
background-color: chocolate;
margin: 5px;
}
.pageination .page.active{
background-color: greenyellow;
}
</style>
</head>
<body>
<ul>
{% for i in data %}
<li> {{ i }}</li>
{% endfor %}
</ul>
<br>
<div class="pageination">
{{ num|safe }}
</div>
</body>
</html>
二、讲解
对页数限制的讲解
page_count 显示条数
current_page 当前页数
page_num 总页数
11 页码
总页数小于实际展示页数
总页数大于实际展示页数
前提条件总页数11时
当前页<=6
开始页1
结束页11
当前页>6
开始页=当前页-5
结束页=当前页+6
当前页+5>总页数
结束页=总页数+1
开始页=总页数-10
转换成代码
if page_num<11:
start_index = 1
end_index = page_num
else:
if current_page<=6:
start_index = 1
end_index = 11
else:
start_index = current_page -5
end_index = current_page +6
if current_page+5>page_num:
end_index = page_num+1
start_index = page_num-10
上一页/下一页操作
上一页
当前页等于1时
上一页功能失效
当前页大于1时
当前页-1
下一页
当前页等于总页数时
下一页功能失效
当前页小于总页数时
当前页+1
代码如下
if current_page ==1:
prev = '<a class='page' href="javascript:viod(0)">上一页</a>'
else:
prev = '<a class='page' href="/tp5/?page=%s">上一页</a>'%(current_page-1)
if current_page ==page_num:
prev = '<a class='page' href="javascript:viod(0)">下一页</a>'
else:
prev = '<a class='page' href="/tp5/?page=%s">下一页</a>'%(current_page+1)
跳转代码如下:
jump ='''
<input type='text' /><a onclick='jumpTo(this, "/tp5/?page=");' id='ii'>GO</a>
<script>
function jumpTo(ths,base){
var val = ths.previousSibling.value;
location.href = base + val;
}
</script>
'''
分页整体代码如下:
def user_list(request):
page_count = 10
#当page 关键字不存在时 默认1
current_page = request.GET.get('page',1)
current_page = int(current_page)
#页数减-1*10,切片的起始位置
start = (current_page-1)*page_count
# 页数减*10,切片的结束位置
end = (current_page)*page_count
data = li[start:end]
#显示页数,因为页面没有循环数字的方法,所以后台处理
page_num,mod_num = divmod(len(li),page_count)
#余数不为0页数加一
if mod_num:
page_num += 1
temp_list = []
#显示最大页数
max_page = 11
if page_num<max_page:
start_index =1
end_indx =max_page
else:
if current_page<=(max_page+1)/2:
start_index = 1
end_indx = max_page
else:
start_index = current_page-(max_page-1)/2
end_indx = current_page+(max_page+1)/2
if current_page+(max_page-1)/2>page_num:
start_index = page_num-max_page+1
end_indx = page_num+1
if current_page == 1:
prev = '<a class="page" href="javascript:viod(0)">上一页</a>'
else:
prev = '<a class="page" href="/tp5/?page=%s">上一页</a>' % (current_page - 1)
temp_list.append(prev)
# 起始页
for i in range(int(start_index),int(end_indx)):
if i==current_page:
temp = '<a class="page active" href="/tp5/?page=%s">%s</a>'%(i,i)
else:
temp = '<a class="page" href="/tp5/?page=%s">%s</a>'%(i,i)
temp_list.append(temp)
if current_page == page_num:
prev = '<a class="page" href="javascript:viod(0)">上一页</a>'
else:
prev = '<a class="page" href="/tp5/?page=%s">上一页</a>' % (current_page + 1)
temp_list.append(prev)
jump ='''
<input type='text' /><a onclick='jumpTo(this, "/tp5/?page=");' id='ii'>GO</a>
<script>
function jumpTo(ths,base){
var val = ths.previousSibling.value;
location.href = base + val;
}
</script>
'''
temp_list.append(jump)
temp_num = ''.join(temp_list)
return render(request,'user_list.html',{'num':temp_num,'data':data})
三、打包成类
class page():
'''
start:获取每页的第一条数据
end:获取每页的最后一条数据
all_count: 获取总页数
page_str: 分页展示,上一页,下一页,跳转
'''
def __init__(self,current_page,date_count,page_count=10,max_page = 11):
'''
:param current_page: 当前页数,通过request.GET获取
:param date_count: 数据总个数,用divmod进行取余判断总页数
:param page_count: 每页显示数据的个数
:param max_page: 网页可显示的最大总页数
'''
self.current_page = current_page
self.data_count = date_count
self.page_count = page_count
self.max_page = max_page
@property
def start(self):
'''
:return: (当前页-1)*每页展示条数==每页展示数据的第一条
'''
return (self.current_page-1)*self.page_count
@property
def end(self):
'''
:return: 当前页*每页展示条数==每页展示数据的最后一条
'''
return self.current_page*self.page_count
@property
def all_count(self):
'''
:param page_all: 取余的方法,得到商
:param mod_num: 取余的方法,得到余
:return: 返回实际总页数
'''
v =self.data_count
page_all, mod_num = divmod(v, self.page_count)
if mod_num:
page_all += 1
return page_all
def page_str(self):
temp_list = []
if self.all_count < self.max_page:
start_index = 1
end_indx = self.max_page
else:
if self.current_page <= (self.max_page + 1) / 2:
start_index = 1
end_indx =self.max_page
else:
start_index = self.current_page - (self.max_page - 1) / 2
end_indx = self.current_page + (self.max_page + 1) / 2
if self.current_page + (self.max_page - 1) / 2 > self.all_count:
start_index = self.all_count - self.max_page + 1
end_indx = self.all_count + 1
if self.current_page == 1:
prev = '<a class="page" href="javascript:viod(0)">上一页</a>'
else:
prev = '<a class="page" href="/tp5/?page=%s">上一页</a>' % (self.current_page - 1)
temp_list.append(prev)
# 起始页
for i in range(int(start_index), int(end_indx)):
if i == self.current_page:
temp = '<a class="page active" href="/tp5/?page=%s">%s</a>' % (i, i)
else:
temp = '<a class="page" href="/tp5/?page=%s">%s</a>' % (i, i)
temp_list.append(temp)
if self.current_page ==self.all_count:
prev = '<a class="page" href="javascript:viod(0)">上一页</a>'
else:
prev = '<a class="page" href="/tp5/?page=%s">上一页</a>' % (self.current_page + 1)
temp_list.append(prev)
jump = '''
<input type='text' /><a onclick='jumpTo(this, "/tp5/?page=");' id='ii'>GO</a>
<script>
function jumpTo(ths,base){
var val = ths.previousSibling.value;
location.href = base + val;
}
</script>
'''
temp_list.append(jump)
temp_num = ''.join(temp_list)
return temp_num
使用类
def user_list(request):
page_count = 10
#当page 关键字不存在时 默认1
current_page = request.GET.get('page',1)
current_page = int(current_page)
#创建分页对象
page_obj = page(current_page,len(li))
#每页数据的起始数据,结束数据
data = li[page_obj.start:page_obj.end]
temp_num=page_obj.page_str()
return render(request,'user_list.html',{'num':temp_num,'data':data})
-
直接使用版本
__author__ = 'Administrator'
from django.utils.safestring import mark_safe
class Pagination(object):
def __init__(self, current_page, data_count, per_page_count=1, pager_num=7):
try:
self.current_page = int(current_page)
except Exception as e:
self.current_page = 1
self.data_count = data_count
self.per_page_count = per_page_count
self.pager_num = pager_num
@property
def start(self):
return (self.current_page - 1) * self.per_page_count
@property
def end(self):
return self.current_page * self.per_page_count
@property
def total_count(self):
v, y = divmod(self.data_count, self.per_page_count)
if y:
v += 1
return v
def page_str(self, base_url):
page_list = []
if self.total_count < self.pager_num:
start_index = 1
end_index = self.total_count + 1
else:
if self.current_page <= (self.pager_num + 1) / 2:
start_index = 1
end_index = self.pager_num + 1
else:
start_index = self.current_page - (self.pager_num - 1) / 2
end_index = self.current_page + (self.pager_num + 1) / 2
if (self.current_page + (self.pager_num - 1) / 2) > self.total_count:
end_index = self.total_count + 1
start_index = self.total_count - self.pager_num + 1
if self.current_page == 1:
prev = '<li><a class="page" href="javascript:void(0);">上一页</a></li>'
else:
prev = '<li><a class="page" href="%s?p=%s">上一页</a></li>' % (base_url, self.current_page - 1,)
page_list.append(prev)
for i in range(int(start_index), int(end_index)):
if i == self.current_page:
temp = '<li class="active"><a class="page active" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
else:
temp = '<li><a class="page" href="%s?p=%s">%s</a></li>' % (base_url, i, i)
page_list.append(temp)
if self.current_page == self.total_count:
nex = '<li><a class="page" href="javascript:void(0);">下一页</a></li>'
else:
nex = '<li><a class="page" href="%s?p=%s">下一页</a></li>' % (base_url, self.current_page + 1,)
page_list.append(nex)
# jump = """
# <input type='text' /><a onclick='jumpTo(this, "%s?p=");'>GO</a>
# <script>
# function jumpTo(ths,base){
# var val = ths.previousSibling.value;
# location.href = base + val;
# }
# </script>
# """ % (base_url,)
#
# page_list.append(jump)
page_str = mark_safe("".join(page_list))
return page_str
网友评论