Search code examples
djangodjango-generic-views

Why do I get an error "object of type 'HttpResponseRedirect' has no len()"?


In my Django app i have a class-based view that inherits ListView. get_queryset method is overriden this way:

def get_queryset(self):
        if not 'anonymous_nickname' in self.request.session:
            return HttpResponseRedirect('/')
        filters = {}
        if self.request.user.is_anonymous():
            filters['allow_anonymous_access'] = True
        return CoopRoom.objects.filter(**filters)

in order not to let user see list of rooms when he wants to reach them without entering any data on the main page. But instead of performing this redirect:

            return HttpResponseRedirect('/')

i get an error "object of type 'HttpResponseRedirect' has no len()". I cannot see the reason why?

Thanks in advance.


Solution

  • The get_queryset() method should return a queryset (like you do in your second return statement) and not an HttpResponseRedirect !

    Instead of returning the HttpResponseRedirect you can either throw a 404 exception (raise Http404) or return an empty queryset with return CoopRoom.objects.none().