首页 > 解决方案 > 我不知道为什么我的页面没有分页我没有收到任何错误请帮助:)

问题描述

该页面应该分页,但我不知道我做错了什么。如果有人能帮我弄清楚,我将不胜感激

这是一个网站上的评论部分,我真的不知道如何解决它。我一直在网上查找问题没有结果然后来到这里

from django.core.paginator import Paginator

def Home_view(request):

    posts = Post.objects.order_by("-date_posted")
    all_experiences = Experience.objects.order_by("-id")
    all_educations = Education.objects.order_by("-id")
    all_skills = Skill.objects.all()
    paginator = Paginator(posts, 1)
    page = request.GET.get('page')
    post_list = paginator.get_page(page)

    context = {

        'posts': posts,

        'all_experiences': all_experiences,

        'all_educations': all_educations,

        'all_skills': all_skills,

    }
    return render(request, 'resume.html', context)

Html页面应该分页

{% if is_paginated %}
<div class="pagination">
    <span class="step-links">
        {% if post_list.has_previous %}
        <a href="?page=1">&laquo; first</a>
        <a href="?page={{ post_list.previous_page_number }}">previous
        </a>
        {% endif %}

        <span class="current">
           Page{{post_list.number}}of{{post_list.paginator.num_pages}}.
        </span>

        {% if post_list.has_next %}
        <a href="?page={{ post_list.next_page_number }}">next</a>
        <a href="?page={{ post_list.paginator.num_pages }}">last&raquo;
        </a>
        {% endif %}
    </span>
</div>
{% endif %}
{% else %}

该页面应该一次显示 5 个帖子,但不会也不会抛出任何错误,它只是不起作用

标签: djangodjango-views

解决方案


看起来你没有添加post_list到你的上下文中,所以{% if post_list.has_previous %}什么都不做。您正在传递posts未分页的帖子列表。您还需要添加is_paginated到上下文中。尝试更新context为:

context = {

        'all_experiences': all_experiences,

        'all_educations': all_educations,

        'all_skills': all_skills,

        'post_list': post_list,

        'is_paginated': True,

    }

推荐阅读