如何在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">&laquo; 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 &raquo;</a>
        {% endif %}
    </span>
</div>
posted @ 2023-03-23 09:12  super_ip  阅读(184)  评论(0编辑  收藏  举报