How to write a basic try/except in a Django Generic Class View

django, django-views

Solution

A try except block in the `get_queryset` method isn't really appropriate. Firstly, `Model.objects.filter()` won't raise an exception if the queryset is empty - it just returns an empty queryset. Secondly, the `get_queryset` method is meant to return a queryset, not an `HttpResponse`, so if you try to redirect inside that method, you'll run into problems.

I think you might find it easier to write a function based view. A first attempt might look like this:

from django.shortcuts import render

def my_view(request):
    """
    Display all the objects belonging to the user 
    that are not done, or redirect if there are not any,
    """
    objects = Model.objects.filter(user=self.request.user, done=False)
    if not objects:
        return HttpResponseRedirect("/empty-queryset-url/")
    return render(request, 'myapp/template.html', {"objects": objects})

The advantage is that the flow of your function is pretty straight forward. This doesn't have as many features as the `ListView` generic class based view (it's missing pagination for example), but it is pretty clear to anyone reading your code what the view is doing.

If you really want to use the class based view, you have to dig into the CBV documentation for multiple object mixins and the source code, and find a suitable method to override.

In this case, you'll find that the `ListView` behaviour is quite different to what you want, because it never redirects. It displays an empty page by default, or a 404 page if you set `allow_empty = False`. I think you would have to override the `get` method to look something like this (untested).

class MyView(ListView):
    def get_queryset(self):
        return Model.objects.filter(user=self.request.user, done=False)

    def get(self, request, *args, **kwargs):
        self.object_list = self.get_queryset()

        if len(self.object_list == 0):
            return HttpResponseRedirect("/empty-queryset-url/")
        context = self.get_context_data(object_list=self.object_list)
        return self.render_to_response(context)

Problem

I'd like to write an except clause that redirects the user if there isn't something in a queryset. Any suggestions welcome. I'm a Python noob, which I get is the issue here. Here is my current code: ``` def get_queryset(self): try: var = Model.objects.filter(user=self.request.user, done=False) except: pass return var ``` I want to do something like this: ``` def get_queryset(self): try: var = Model.objects.filter(user=self.request.user, done=False) except: redirect('add_view') return var ```

Original source