How to redirect on conditions with class based views in Django 1.3?

I know this is old, but I actually agree with Tommaso. The dispatch() method is what handles the request and returns the HTTP response. If you want to adjust the response of the view, thats the place to do it. Here are the docs on dispatch().

class VideosView(ListView):
    # use model manager
    queryset = Videos.on_site.all()

    def dispatch(self, request, *args, **kwargs):
        # check if there is some video onsite
        if not queryset:
            return redirect('other_page')
        else:
            return super(VideosView, self).dispatch(request, *args, **kwargs)

    # other method overrides here

Leave a Comment

tech