我试图对页面进行分页,以便每页显示1个状态...分页失败,并且不显示指向下一页的链接
views.py
def post_list(request):
posts= Post.objects.all()
paginator = Paginator(post_list, 1)
page = request.GET.get('page')
return render(request, 'news.html', {'posts':posts})
模板
{% for num in posts.paginator.page_range %}
{% if posts.number == num %}
<strong>{{ num }}</strong>
{% else %}
{{ num }}
{% endif %}
{% endfor %}
答案 0 :(得分:0)
posts
仍然是Post.objects.all(..)
查询集,您仍然需要使用.get_page(..)
[Django-doc]来检索页面:
from django.core.paginator import Paginator
def post_list(request):
post_list = Post.objects.all()
paginator = Paginator(posts, 1)
page = request.GET.get('page')
posts = paginator.get_page(page)
return render(request, 'news.html', {'posts':posts})
如果不创建页面,您要做的不只是构建pagiantor,然后将其丢弃。
因此,您可以在模板中将其呈现为:
<strong>{{ posts.number }}</strong>