效果如下:
脚本:
1. 脚本结构
2.pagination.py
from django.utils.safestring import mark_safe class Page:
'''
current_page: 当前页码
data_count: 数据总个数
per_page_count: 每页展示几个数据
pager_num: 展示页码个数
'''
def __init__(self, current_page, data_count, per_page_count=5, pager_num=6):
self.current_page = current_page
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 = [] first = '<li><a class="page" href="%sp=%s">首页</a></li>' % (base_url, 1,)
page_list.append(first) 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);" style="display: none">上一页</a></li>'
else:
prev = '<li><a class="page" href="%sp=%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 " href="%sp=%s">%s</a></li>' % (base_url, i, i)
else:
temp = '<li ><a class="page" href="%sp=%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);" style="display: none">下一页</a></li>'
else:
nex = '<li><a class="page" href="%sp=%s">下一页</a></li>' % (base_url, self.current_page + 1,)
page_list.append(nex) last = '<li><a class="page" href="%sp=%s">尾页</a></li>' % (base_url, self.total_count,)
page_list.append(last) total_record = '<span style="display: inline-block;margin-top: 10px;">共%s条记录,共%s页,每页%s条,第%s页</span>' % (self.data_count, self.total_count, self.per_page_count,self.current_page)
page_list.append(total_record)
page_str = mark_safe("".join(page_list)) return page_str
3. view.py
def page(req):
shop_list = ['apple','apple2','apple3','apple4','apple5','apple6','apple7','apple8','apple9']
current_page = req.GET.get('p', 1)
current_page = int(current_page)
page_obj = pagination.Page(current_page, len(shop_list), 2, 4) data = shop_list[page_obj.start:page_obj.end]
print("data:%s"%data) # data:['apple', 'apple2'] page_str = page_obj.page_str('/mytest/page/?')
context = {'page_str':page_str,'data':data}
return render(req,'page.html',context)
4.html.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>分页</title>
<link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style>
ul li{float:left;}
</style>
</head>
<body>
<ul>
{% for i in data %}
<li>{{i}}</li>
{% endfor %}
</ul>
<div style="margin-left: 270px" >
<ul class="pagination">
{{page_str}}
</ul>
</div> </body>
</html>
5.url.py
from django.conf.urls import url
from mytest import views urlpatterns = [
# page
url(r'page/$', views.page),
]