Python 在模板中未显示的视图函数中添加分页
我试图将分页添加到函数中的通知列表中,因此我在中的文档后面的函数中添加了分页器,但模板中没有显示分页。 views.py:Python 在模板中未显示的视图函数中添加分页,python,django,pagination,Python,Django,Pagination,我试图将分页添加到函数中的通知列表中,因此我在中的文档后面的函数中添加了分页器,但模板中没有显示分页。 views.py: def ShowNotifications(request): user=request.user notifications= Notification.objects.filter(user=user).order_by('-date') template= loader.get_template('notifications/notificat
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-->
{%if已分页%}
{%如果页面_obj.has_previous%}
-
{%endif%}
-
{%如果页面_obj.has_next%}
-
{%endif%}
{%endif%}
我在列表视图中使用了相同的分页html,并且它工作正常,所以这是第一次在函数中添加它
我的问题:我做错了什么,阻止分页出现
我怎么修理它?我正处于学习阶段,所以一些解释可能会有用。我的错误是,当循环通过obj时,我没有使用
page\u obj
:
{% for contact in page_obj %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
{%用于第_obj%页中的联系人]
{#每个“联系人”都是一个联系人模型对象。#
{{contact.full|u name | upper}}
...
{%endfor%}
谢谢我的错误是,当循环通过obj时,我没有使用
page\u obj
,应该这样:
{% for contact in page_obj %}
{# Each "contact" is a Contact model object. #}
{{ contact.full_name|upper }}<br>
...
{% endfor %}
{%用于第_obj%页中的联系人]
{#每个“联系人”都是一个联系人模型对象。#
{{contact.full|u name | upper}}
...
{%endfor%}
谢谢试试这个:
page\u number=request.GET.GET('page',1)
。如果找不到页面查询,它会将页码设置为1
。@AjayLingayat what nothing happenedTry:page\u number=request.GET.GET('page',1)
。如果找不到页面查询,它会将页码设置为1
。@AjayLingayat什么都没有发生