I am using a ListView that list videos according to tags. The filtering happens in get_queryset(). I'd like to redirect the user to another page if the tag doesn't contains any video.

With a function, it would be easy. Query, check the queryset, redirect. With a class, I fail doing so:

class VideosView(generic.ListView):

    def get_queryset(self):
        """
            This work.
        """

        tag = self.kwargs.get('tag', None)

        self.videos = Video.on_site.all()

        if tag:
            self.videos = Video.tagged.with_all(tag, self.videos)

        return self.videos

    def get(self, request, *args, **kwargs):
        """
        This doesn't work because self.videos doesn't exist yet.
        """
        if not self.videos:
            return redirect('other_page')

        return super(Videos, self).get(request, *args, **kwargs)
Edit
Report