我不知道为什么我的页面没有分页,我没有得到任何错误,请帮助:)

时间:2019-08-13 23:29:46

标签: django django-views

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

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

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个帖子,但不会并且不会抛出任何错误,只是不起作用

1 个答案:

答案 0 :(得分:0)

您似乎没有将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,

    }
相关问题