object of type 'NoneType' has no len() in django ClassBased ListView

django, django-class-based-views, listview, nonetype

Solution

The method `get_queryset` doesn't return anything when you haven't filled something in for `q` in `request.GET`. Hence it returns `None` which isn't a valid QuerySet or iterable. As such Django can't call `len()` on it.

You need to ensure `get_queryset` always returns a value:

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

Problem

I have the below view for displaying list of searched records ``` 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 ``` 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() ``` SearchForm ``` <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> ``` So the above code was working when we enter any value for search, i mean if there is any value for `q`, but when we submit the `Search form` without anything, i was getting above error So why am i getting it and how to avoid it ?

Original source