Django:分页器错误:“用户”类型的对象没有len()

时间:2018-08-24 03:37:23

标签: django django-class-based-views paginator

我正在学习使用分页器,因为我的列表越来越长!我遇到错误时无法执行它。我注意到,如果删除paginate_by = 10,错误就会消失,但是我敢肯定这是分页所必需的。为什么get_queryset与分页程序发生冲突。

class NotificationsListView(ListView):
    template_name = "notices/list.html"
    paginate_by = 10

    def get_context_data(self, *args, **kwargs):
        context = super(NotificationsListView, self).get_context_data(*args, **kwargs)
        qs = self.get_queryset().notifications.all()
        paginator = Paginator(qs , self.paginate_by)
        page = self.request.GET.get('page')
        try:
            notification_pages = paginator.page(page)
        except PageNotAnInteger:
            notification_pages = paginator.page(1)
        except EmptyPage:
            notification_pages = paginator.page(paginator.num_pages)
        context['notifications'] = notification_pages
            return context    
        def get_queryset(self, *args, **kwargs):
        request = self.request
        return User.objects.get(pk=self.request.user.pk)

跟踪:

File "myapp\lib\site-packages\django\core\paginator.py" in count
  85.             return self.object_list.count()

During handling of the above exception ('User' object has no attribute 'count'), another exception occurred:

File "myapp\lib\site-packages\django\core\handlers\exception.py" in inner
  35.             response = get_response(request)

File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

File "myapp\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "myapp\lib\site-packages\django\views\generic\base.py" in view
  69.             return self.dispatch(request, *args, **kwargs)

File "myapp\lib\site-packages\django\views\generic\base.py" in dispatch
  89.         return handler(request, *args, **kwargs)

File "myapp\lib\site-packages\django\views\generic\list.py" in get
  157.         context = self.get_context_data()

File "myapp\src\notices\views.py" in get_context_data
  18.         context = super(NotificationsListView, self).get_context_data(*args, **kwargs)

File "myapp\lib\site-packages\django\views\generic\list.py" in get_context_data
  119.             paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)

File "myapp\lib\site-packages\django\views\generic\list.py" in paginate_queryset
  69.             page = paginator.page(page_number)

File "myapp\lib\site-packages\django\core\paginator.py" in page
  65.         number = self.validate_number(number)

File "myapp\lib\site-packages\django\core\paginator.py" in validate_number
  43.         if number > self.num_pages:

File "myapp\lib\site-packages\django\utils\functional.py" in __get__
  36.         res = instance.__dict__[self.name] = self.func(instance)

File "myapp\lib\site-packages\django\core\paginator.py" in num_pages
  95.         if self.count == 0 and not self.allow_empty_first_page:

File "myapp\lib\site-packages\django\utils\functional.py" in __get__
  36.         res = instance.__dict__[self.name] = self.func(instance)

File "myapp\lib\site-packages\django\core\paginator.py" in count
  90.             return len(self.object_list)

Exception Type: TypeError at /notices/
Exception Value: object of type 'User' has no len()

2 个答案:

答案 0 :(得分:1)

get_queryset应该返回queryset而不是object

还有另一种称为get_object的方法,该方法返回一个对象。

答案 1 :(得分:1)

您要显示特定用户的通知列表,对吗?然后,您应该从UIScrollView方法返回通知列表。

我认为您应该这样,并且从我的角度来看,您不应该自己处理分页,django / Django-restframework应该在内部进行处理。

get_queryset

使用Django Rest框架,您只需要在设置文件中添加默认的分页器,就像这样。

class NotificationsListView(ListView):
    template_name = "notices/list.html"
    paginate_by = 10

    def get_queryset(self, *args, **kwargs):
        return self.request.user.notifications.all()

或者,如果您想使用现有代码,则可以尝试如下操作:

REST_FRAMEWORK = {
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 50,
}