自定制分页

pager.py

#_author:wenghao
#date:2019/8/5

class Pagination(object):
def __init__(self,totalCount,currentPage,perPageItemNum=20,maxPageNum=7):
# 总页数
self.total_count=totalCount
# 当前页
try:
self.current_page=int(currentPage)
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
def num_pages(self):
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)
# 当前页面小于中间值part的时候
if(self.current_page<part):
return range(1,self.max_page_num+1)
# 当页面是最后页面小于当前页加上part时
if(self.current_page+part>self.num_pages):
return range(self.num_pages-self.max_page_num,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></li>" % (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)


index3.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css" />
</head>
<body>
<ul>
{% for row in data %}
<li>{{ row.name }}-{{ row.age }}</li>
{% endfor %}
</ul>

{% for i in page_obj.pager_num_range %}
<a href="/index3.html?p={{ i }}">{{ i }}</a>
{% endfor %}

<ul class="pagination pagination-sm">
{{ page_obj.page_str|safe }}
</ul>

{# {% if posts.has_previous %}#}
{# <a href="/index.html?p={{ posts.previous_page_number }}">上一页</a>#}
{# {% else %}#}
{# <a href="#">上一页</a>#}
{# {% endif %}#}
{##}
{# {% if posts.has_next %}#}
{# <a href="/index.html?p={{ posts.next_page_number }}">下一页</a>#}
{# {% endif %}#}
{# <span>#}
{# {{ posts.number }}-{{ posts.paginator.num_pages }}#}
{# </span>#}
</body>
</html>


view
def index3(request):
from app01.pager1 import Pagination
current_page=request.GET.get('p')#传入现在是第几页
page_obj=Pagination(666,current_page)
data_list=USER_LIST[page_obj.start():page_obj.end()]
return render(request,'index3.html',{'data':data_list,'page_obj':page_obj})
posted on 2019-08-05 16:12  帅的遭人砍see  阅读(129)  评论(0编辑  收藏  举报