【问题标题】:I don't know why my page isn't paginating i don't get any errors pls help :)我不知道为什么我的页面没有分页我没有收到任何错误请帮助:)
【发布时间】:2019-08-13 23:29:46
【问题描述】:

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

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

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

【问题讨论】:

  • 您应该将post_list 添加到渲染上下文中。 is_paginated 也不见了。

标签: django django-views


【解决方案1】:

看起来您没有将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,

    }

【讨论】:

  • 感谢您的回答帮助了我,但 html 仍然显示不正确的帖子数量,但分页器的逻辑工作正常,显示正确的页面数量但帖子数量错误
猜你喜欢
  • 1970-01-01
  • 2022-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多