首页 > 解决方案 > 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它正在工作并显示页码。现在我可以点击页码。

标签: django

解决方案


您应该使用当前页面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 %}

推荐阅读