类型为'HttpResponse'的对象的Django分页错误没有len()

时间:2018-11-13 07:43:47

标签: python django

我正在基于自己的观点开发使Django对我的产品列表进行分页的功能。没有分页,我的代码就可以正常工作。这是我在分页之前的代码

class JobListIndex2(TagMixin, ListView):
    template_name = 'jobs/job_list.html'
    model = Job
    paginate_by = 10
    context_object_name = 'job'

    def get_queryset(self, *args, **kwargs):
        request = self.request
        return Job.objects.all()

然后我决定集成分页并将代码更新为此

class JobListIndex2(TagMixin, ListView):
    template_name = 'jobs/job_list.html'
    model = Job
    paginate_by = 10
    context_object_name = 'job'

    # def get_queryset(self, *args, **kwargs):
    #     request = self.request
    #     return Job.objects.all()

    def get_queryset(self, *args, **kwargs):
        request = self.request
        queryset_list = Job.objects.all().order_by("-time_starting")
        paginator = Paginator(queryset_list, 10)  # Show 10 jobs per page

        page = request.GET.get('page')
        queryset = paginator.get_page(page)

        context = {
            'job': queryset,
            'title': 'Jobs'
        }
        return render(request, 'jobs/job_list.html', context)

产生的错误与类型为'HttpResponse'的对象有关,没有len(),我不确定这是从哪里来的。我的回溯

Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\views\generic\list.py", line 157, in get
    context = self.get_context_data()
  File "C:\Users\User\Desktop\protectandserve\jobs\views.py", line 371, in get_context_data
    context = super(TagMixin, self).get_context_data(**kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\views\generic\list.py", line 119, in get_context_data
    paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\views\generic\list.py", line 69, in paginate_queryset
    page = paginator.page(page_number)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\paginator.py", line 67, in page
    number = self.validate_number(number)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\paginator.py", line 45, in validate_number
    if number > self.num_pages:
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\paginator.py", line 97, in num_pages
    if self.count == 0 and not self.allow_empty_first_page:
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django-2.1.1-py3.7.egg\django\core\paginator.py", line 92, in count
    return len(self.object_list)
TypeError: object of type 'HttpResponse' has no len()
[13/Nov/2018 07:42:46] "GET /jobs/jobs HTTP/1.1" 500 115769

请问有什么提示吗?

1 个答案:

答案 0 :(得分:1)

方法get_queryset必须返回QuerySet对象。在您的情况下,您尝试返回HttpResponse。

但是在您的情况下,您不需要使用分页类。如果您指定ListView属性,则paginate_by会自动为您完成操作。

如果您想在模板中使用更多上下文

def get_context_data(self, **kwargs):
    context = super(JobListIndex2, self).get_context_data(**kwargs)
    context['title'] = 'Jobs'
    return context