django 分页例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了django 分页例子相关的知识,希望对你有一定的参考价值。
django 分页例子
安装
python3.6 -m pip install django-pure-pagination==0.3.0
设置
settings.py
INSTALLED_APPS = [
‘pure_pagination‘,
]
PAGINATION_SETTINGS = {
‘PAGE_RANGE_DISPLAYED‘: 3,
‘MARGIN_PAGES_DISPLAYED‘: 2,
‘SHOW_FIRST_PAGE_WHEN_INVALID‘: True,
}
DISPLAY_PER_PAGE = 15
应用
from pure_pagination import PageNotAnInteger
from pure_pagination import Paginator
try:
page = self.request.GET.get(‘page‘, 1)
except PageNotAnInteger_He as e:
page = 1
p = Paginator_He(self.queryset, getattr(settings, ‘DISPLAY_PER_PAGE‘), request=self.request)
asset_list = p.page(page)
context = {
"ecs_list": asset_list,
}
<div style="text-align:center;">
<nav class="pagination">
<li><a href="{% url "asset:ecs_list" %}?page=1">首页 </a></li>
{% if ecs_list.has_previous %}
<li class="long"><a
href="?{{ ecs_list.previous_page_number.querystring }}">上一页</a>
</li>
{% endif %}
{% for page in ecs_list.pages %}
{% if page %}
{% ifequal page ecs_list.number %}
<li class="active"><a href="?{{ page.querystring }}">{{ page }}</a>
</li>
{% else %}
<li><a href="?{{ page.querystring }}" class="page">{{ page }}</a>
</li>
{% endifequal %}
{% else %}
<li class="none"><a href="">...</a></li>
{% endif %}
{% endfor %}
{% if ecs_list.has_next %}
<li class="long"><a
href="?{{ ecs_list.next_page_number.querystring }}">下一页</a>
</li>
{% endif %}
<li>
<a href="{% url "asset:ecs_list" %}?page={{ ecs_list.paginator.num_pages }}">尾页 </a>
</li>
<li><span
style="color: #0a0a0a">总: ?{{ ecs_list.paginator.num_pages }} 页</span>
</li>
<li><span
style="color: #0a0a0a"> ECS数量: {{ ecs_count }}</span>
</li>
</nav>
</div>
以上是关于django 分页例子的主要内容,如果未能解决你的问题,请参考以下文章