【发布时间】: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">« 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»
</a>
{% endif %}
</span>
</div>
{% endif %}
{% else %}
该页面应该一次显示 5 个帖子,但不会也不会抛出任何错误,它只是不起作用
【问题讨论】:
-
您应该将
post_list添加到渲染上下文中。is_paginated也不见了。
标签: django django-views