实现自定制页码数类型
class myPaginator(Paginator):
def __init__(self,curr_page,per_page_num,*args,**kwargs):
self.curr_page=int(curr_page)
self.per_page_num=int(per_page_num)
super(myPaginator,self).__init__(*args,**kwargs)
def page_num(self):
curr_page=self.curr_page #当前页面
full_page=self.num_pages #总页数
per_page_num=self.per_page_num #显示页码数
half_per_page=int(per_page_num/2)
print(curr_page,full_page,per_page_num,half_per_page)
if curr_page<=half_per_page:
return range(1,per_page_num+1)
elif curr_page>=(full_page-half_per_page):
return range(full_page-per_page_num,full_page)
else:
return range(curr_page-half_per_page,curr_page+half_per_page)
veiws:--------------------------------------------------------------------------------------
paginator=myPaginator(curr_page,10,root_lst,10)#(当前页码,一页中显示的页码数个数,内容,一页中的内容)
try:
posts=paginator.page(curr_page)
except EmptyPage:
posts=paginator.page(1)
except PageNotAnInteger:
posts=paginator.page(paginator.num_pages)
html:---------------------------------------------------------------------------------------------------
{% for foo in posts.object_list %} #遍历一页中的内容
<li>{{ foo.name }} - {{ foo.age }}</li> #一页中的其中一条内容
{% endfor %}
{% if posts.has_previous %} #当不存在上一页时的处理方法
<a href="/index?p={{ posts.previous_page_number }}">上一页</a>
{% else %}
<a href="#">上一页</a>
{% endif %}
{% for i in posts.paginator.page_num %} #显示的页码
{% if i == posts.number %}
<span style="display: inline-block;width:20px">{{i}}</span>
{% else %}
<a href="/index?p={{i}}" style="display: inline-block;width:20px">{{i}}</a>
{% endif %}
{% endfor %}
{% if posts.has_next %} #当不存在下一页时的处理方法
<a href="/index?p={{ posts.next_page_number }}">下一页</a>
{% else %}
<a href="#">下一页</a>
{% endif %}