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



我正在我的视图上开发让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:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorehandlersexception.py", line 34, in inner
    response = get_response(request)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorehandlersbase.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorehandlersbase.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoviewsgenericbase.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoviewsgenericbase.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoviewsgenericlist.py", line 157, in get
    context = self.get_context_data()
  File "C:UsersUserDesktopprotectandservejobsviews.py", line 371, in get_context_data
    context = super(TagMixin, self).get_context_data(**kwargs)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoviewsgenericlist.py", line 119, in get_context_data
    paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoviewsgenericlist.py", line 69, in paginate_queryset
    page = paginator.page(page_number)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorepaginator.py", line 67, in page
    number = self.validate_number(number)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorepaginator.py", line 45, in validate_number
    if number > self.num_pages:
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoutilsfunctional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorepaginator.py", line 97, in num_pages
    if self.count == 0 and not self.allow_empty_first_page:
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangoutilsfunctional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:UsersUserAppDataLocalProgramsPythonPython37-32libsite-packagesdjango-2.1.1-py3.7.eggdjangocorepaginator.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

有什么建议吗?

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

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

如果你想在模板中添加额外的上下文,请使用

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

最新更新