如何在django中实现上一页/下一页,分页功能
博客的列表页,如果内容过多,则需要分页,具体操作如下,实际内容以项目本身修改为准:
视图部分:
from django.core.paginator import Paginator
from django.shortcuts import render
def viewprofile(request):
profile_details = Profile.objects.all()
paginator = Paginator(profile_details, 5) # So limited to 5 profiles in a page
page = request.GET.get('page')
profile= paginator.get_page(page) #data
return render(request, 'profile.html', {'profiles': profile}
HTML页面:
{% for profile in profiles %}
{# Here we can display profile details #}
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if profiles.has_previous %}
<a href="?page=1">« first</a>
<a href="?page={{ profiles.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ profiles.number }} of {{ profiles.paginator.num_pages }}.
</span>
{% if profiles.has_next %}
<a href="?page={{ profiles.next_page_number }}">next</a>
<a href="?page={{ profiles.paginator.num_pages }}">last »</a>
{% endif %}
</span>
</div>
本文来自博客园,作者:super_ip,转载请注明原文链接:https://www.cnblogs.com/superip/p/17246203.html