gpt4 book ai didi

django - 'NoneType' 类型的对象在 django ClassBased ListView 中没有 len()

转载 作者:行者123 更新时间:2023-12-01 08:30:34 30 4
gpt4 key购买 nike

我有以下 View 来显示搜索记录列表

class ListOfSearchedRecords(LoginRequiredMixin, ListView):
template_name = 'list_of_searched_records.html'
context_object_name = 'filtered_records'
paginate_by = 10

def get_queryset(self):
"""
Returns the Records
"""
if self.request.method == "GET" and self.request.GET:
if 'q' in self.request.GET:
if self.request.GET['q']:
keyword = self.request.GET.get('q', None)
log.debug("Filtered keyword: %s", keyword)
result = Product.objects.order_by('-created').filter(
Q(products__name__icontains=keyword))
if result:
return result
else:
return []

def get_context_data(self, **kwargs):
context = super(ListOfSearchedRecords, self).get_context_data(**kwargs)
context.update(
{
'context_list':['Data_one', 'Data_two', 'Data_three']}
)
return context

回溯
Traceback (most recent call last):
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 115, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view
return self.dispatch(request, *args, **kwargs)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/views/generic/base.py", line 86, in dispatch
return handler(request, *args, **kwargs)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/views/generic/list.py", line 139, in get
context = self.get_context_data(object_list=self.object_list)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/views/generic/list.py", line 99, in get_context_data
paginator, page, queryset, is_paginated = self.paginate_queryset(queryset, page_size)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/views/generic/list.py", line 53, in paginate_queryset
page = paginator.page(page_number)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/core/paginator.py", line 40, in page
number = self.validate_number(number)
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/core/paginator.py", line 31, in validate_number
if number > self.num_pages:
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/core/paginator.py", line 63, in _get_num_pages
if self.count == 0 and not self.allow_empty_first_page:
File "/home/user/Envs/proj/local/lib/python2.7/site-packages/django/core/paginator.py", line 56, in _get_count
self._count = len(self.object_list)
TypeError: object of type 'NoneType' has no len()

搜索表单
                            <form id="changelist-search" class="form-search" action="{% url 'filtered_list_of_records' %}" method="get">
<input id="searchbar" name="q" type="text" class="search_ico pull-right search_bar input-xlarge fnt16" placeholder="Search">
</form>

所以当我们输入任何搜索值时,上面的代码正在工作,我的意思是如果 q 有任何值,但是当我们提交 Search form 没有任何东西时,我得到了上面的错误

那么为什么我会得到它以及如何避免它?

最佳答案

当您没有为 get_queryset 中的 q 填写内容时, request.GET 方法不会返回任何内容。因此,它返回的 None 不是有效的 QuerySet 或可迭代的。因此,Django 不能对其调用 len()

您需要确保 get_queryset 始终返回一个值:

def get_queryset(self):
"""
Returns the Records
"""
if self.request.method == "GET" and self.request.GET:
if 'q' in self.request.GET:
if self.request.GET['q']:
# ...
return [] # add this line here

关于django - 'NoneType' 类型的对象在 django ClassBased ListView 中没有 len(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20423519/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com