【发布时间】:2020-11-23 07:49:22
【问题描述】:
我正在尝试将分页添加到函数中的通知列表中,因此我在 https://docs.djangoproject.com/en/3.1/topics/pagination/#using-paginator-in-a-view-function 中的文档之后的函数中添加了 Paginator,但模板中没有显示分页。 视图.py:
def ShowNotifications(request):
user=request.user
notifications= Notification.objects.filter(user=user).order_by('-date')
template= loader.get_template('notifications/notifications.html')
paginator = Paginator(notifications, 1) # Show 1 Notification per page.
page_number = request.GET.get('page')
page_obj = paginator.get_page(page_number)
context = {
'notifications': notifications,
'page_obj': page_obj
}
return HttpResponse(template.render(context, request))
这里是分页html
<!--Pagination-->
{% if is_paginated %}
<nav class="d-flex justify-content-center wow fadeIn">
<ul class="pagination pg-blue">
<!--Arrow left-->
{% if page_obj.has_previous %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.previous_page_number }}" aria-label="Previous">
<span aria-hidden="true">«</span>
<span class="sr-only">Previous</span>
</a>
</li>
{% endif %}
<li class="page-item active">
<a class="page-link" href="?page={{ page_obj.number}}">{{ page_obj.number}}
<span class="sr-only">(current)</span>
</a>
</li>
{% if page_obj.has_next %}
<li class="page-item">
<a class="page-link" href="?page={{ page_obj.next_page_number }}" aria-label="Next">
<span aria-hidden="true">»</span>
<span class="sr-only">Next</span>
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
<!--Pagination-->
我在列表视图中使用了相同的分页 html,并且它有效,所以这是第一次将它添加到函数中。
我的问题:我做错了什么导致分页无法出现?
我该如何解决?我正处于学习阶段,所以一些解释可能会有用。
【问题讨论】:
-
试试这个:
page_number = request.GET.get('page', 1)。如果没有找到页面查询,它会将页码设置为1。 -
@AjayLingayat 什么都没发生
标签: python django pagination