Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型为';HttpResponse';没有len()_Python_Django - Fatal编程技术网

Python 类型为';HttpResponse';没有len()

Python 类型为';HttpResponse';没有len(),python,django,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

我正在开发自己的观点,让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

有什么提示吗?

方法
get\u 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

没有必要这样做。ListView已分页。