django - 分页

2018-01-23  本文已影响102人  _琳哥

包:django.core.paginator

Paginator对象
属性
方法
异常exception
Page对象
创建对象
属性
方法
例子
def hero_list(request, pindex):
    if pindex == '':
        pindex = '1'
    heroinfo_list = HeroInfo.objects.all()
    paginatior = Paginator(heroinfo_list, 5)
    page = paginatior.page(int(pindex))
    context = {'page': page}
    return render(request, 'booktest/herolist.html', context)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<ul>
{% for hero in page %}
<li>{{ hero.h_name }}</li>
{% endfor %}
</ul>
<hr/>
{% for index in page.paginator.page_range %}
    {% if index == page.number %}
        {{ index }}
    {% else %}
    <a href="/heroList/{{ index }}">{{ index }}</a>
    {% endif %}
{% endfor %}
</body>
</html>
image.png
上一篇下一篇

猜你喜欢

热点阅读