django - 我不知道为什么我的页面没有分页我没有收到任何错误请帮助:)
问题描述
该页面应该分页,但我不知道我做错了什么。如果有人能帮我弄清楚,我将不胜感激
这是一个网站上的评论部分,我真的不知道如何解决它。我一直在网上查找问题没有结果然后来到这里
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
到你的上下文中,所以{% 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,
}
推荐阅读
- ansible - ansible中是否有任何过滤器可以模仿all()和any()?
- php - PHP DateTime 文本表示
- python - 张量 a (296) 的大小必须与非单维 2 处的张量 b (298) 的大小相匹配
- python - NotImplementedError:无法将符号张量 (lstm_2/strided_slice:0) 转换为 numpy 数组。吨
- hibernate - 将 Spring 数据规范转换为查询
- javascript - 检测特定的 chrome 扩展
- docker - Docker 停止/启动重置为初始密码
- rxjs - 多个 Observable 等待 RetryWhen 上的单个 Observable 完成
- javascript - Cypress - cy.intercept 捕获错误的 url
- php - WooCommerce PDF 发票和装箱单:未使用自定义代码发送附件