django - Django Pagination 不工作并显示每个页面中的所有项目
问题描述
我想每页只显示一个项目,但它显示每页中的所有项目,并且在添加新项目后只增加页码。看图片:
这是我的意见.py
def ShowAuthorNOtifications(request):
user = request.user
notifications = filters.NotificationFilter(
request.GET,
queryset=Notifications.objects.all()
).qs
paginator = Paginator(notifications, 1)
page = request.GET.get('page')
try:
response = paginator.page(page)
except PageNotAnInteger:
response = paginator.page(1)
except EmptyPage:
response = paginator.page(paginator.num_pages)
notification_user = Notifications.objects.filter(user=user).count()
Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
template_name ='blog/author_notifications.html'
context = {
'notifications': notifications,
'notification_user':notification_user,
'page_obj':response,
}
print("##############",context)
return render(request,template_name,context)
过滤器.py
import django_filters
from .models import *
class NotificationFilter(django_filters.FilterSet):
class Meta:
model = Notifications
fields = ['notification_type']
模型.py:
NOTIFICATION_TYPES = (('New Comment','New Comment'),('Comment Approved','Comment Approved')
notification_type = models.CharField(choices=NOTIFICATION_TYPES,max_length=250,default="New Comment")
html
{% for notification in notifications %}
{% if notification.notification_type == "New Comment" %}
#my code......
{%endif%}
{%endfor%}
首先我尝试使用这个 基于函数的视图分页,但得到相同的结果。它只是添加页码并在每页显示所有项目。
#updated 问题object_list
,因为 NKSM 说我的 html 模板
中缺少我。现在我的问题已经解决了,它每页只显示一个项目,但是如何在分页部分显示页码,如 page1、page2、page3。现在它显示这样的分页 Page 1 of 4. next last »
我想点击页码,它会带我进入页面。
#updated2
我在我的 html 中缺少page_obj
我的视图的上下文。添加后page_obj.paginator.page_range
它正在工作并显示页码。现在我可以点击页码。
解决方案
您应该使用当前页面Paginatorobject_list
的( Django Docs ) 。另请参阅Django 文档示例。
我还建议你过滤你notifications
的后端而不是在模板中使用if notification.notification_type == "New Comment"
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
def ShowAuthorNOtifications(request):
user = request.user
notifications = filters.NotificationFilter(
request.GET,
queryset=Notifications.objects.filter(notification_type="New Comment")
).qs
paginator = Paginator(notifications, 1)
page = request.GET.get('page')
try:
cur_page = paginator.page(page)
except PageNotAnInteger:
cur_page = paginator.page(1)
except EmptyPage:
cur_page = paginator.page(paginator.num_pages)
notification_user = Notifications.objects.filter(user=user).count()
Notifications.objects.filter(user=user, is_seen=False).update(is_seen=True)
context = {
'notification_user': notification_user,
'cur_page': cur_page,
}
print("##############",context)
return render(request, 'blog/author_notifications.html', context)
模板.html:
{% for notification in cur_page.object_list %}
...your code...
{% endfor %}
推荐阅读
- android - 如何使用 Google Classroom API 访问 Classroom Course Stream
- unity3d - 更改我的 mac 后在 Unity 中看不到我的项目
- c++ - 检查图连通性的程序会产生分段错误
- c# - xUnit 并行测试集合
- spring-boot - 带有 Spring Boot 单元测试设置的 Apache Camel
- android - 无法使用自定义适配器从 firebase 获取数据,“Сan't pass null for argument pathString in child ()”
- vba - 当用户选择组合框时验证 2 个文本框是否为空
- android - java.net.ConnectException:连接失败
- spring - 蜂窝 + 弹簧靴 2 排除标准字段
- git - 所有提交的 clang-format 以修复缩进并删除尾随空格