在views.py下添加:
def heroinfos_list(request,now_page):
if now_page == "":
now_page = 1
page_size = 2
heroinfos = HeroInfo.objects.all()
# 获取Paginator对象
my_paginator = Paginator(heroinfos,page_size)
#获取page对象,通过page对象的属性获得对应页数的内容
my_page = my_paginator.page(now_page)
response = render(request=request,template_name='book/heroinfos_list.html',context={'my_page':my_page,'title':'显示所有英雄'})
return response
在urls.py文件下原来代码其中一句修改为:
url(r'^heroinfos_list/(\d*)$', views.heroinfos_list),
对应html页面中添加:
<li>
<p>
{% if my_page.has_previous%}
<a href="/heroinfos_list/{{ my_page.previous_page_number }}">上一页</a>
{% endif %}
{% for i in my_page.paginator.page_range %}
{% if my_page.number == i %}
<span>{{ i }}</span>
{% else %}
<a href="/heroinfos_list/{{ i }}">{{ i }}</a>
{% endif %}
{% endfor %}
{% if my_page.has_next%}
<a href="/heroinfos_list/{{ my_page.next_page_number }}">下一页</a>
{% endif %}
</p>
</li>
实现效果:
网友评论