我想在文档的帮助下为具有特定外键的对象显示分页,我正在使用这个:
def baslik_handle(request, title):
baslik, created = Baslik.objects.get_or_create(title=title)
entryler2 = Entry.objects.filter(baslik=baslik).order_by('timestamp')
paginator = Paginator(entryler2, 10)
page = request.GET.get('page')
try:
sayfalar = paginator.page(page)
except PageNotAnInteger:
sayfalar = paginator.page(1)
except EmptyPage:
sayfalar = paginator.page(paginator.num_pages)
ctx = {'entryler2':entryler2, 'sayfalar':sayfalar}
return render(request, "baslik/tek.html", ctx)
对于 HTML:
{% for entry in entryler2 %}
{{ entry }}<br>
{% endfor %}
<div class="pagination">
<span class="step-links">
{% if sayfalar.has_previous %}
<a href="?page={{ sayfalar.previous_page_number }}"><i class="fa fa-arrow-left"></i></a>
{% endif %}
<span class="current">
{{ sayfalar.number }} / {{ sayfalar.paginator.num_pages }}
</span>
{% if sayfalar.has_next %}
<a href="?page={{ sayfalar.next_page_number }}"><i class="fa fa-arrow-right"></i></a>
{% endif %}
</span>
</div>
毕竟这个分页不起作用。它只是计算页码,而不是将对象分隔到页面。例如,我有 26 个具有相同 Baslik 外键的“条目”。当我查看它的页面时,它只列出了所有 3 个页面中的所有 26 个“条目”。我的代码有什么问题?我应该怎么做才能修复它?谢谢。