Py学习  »  Django

“HttpResponse”类型的对象处的Django分页错误没有len()

project_kingz • 7 年前 • 2062 次点击  

我正在开发我的观点的一种能力,让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

有小费吗?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39805
文章 [ 1 ]  |  最新文章 7 年前