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)
You should just name your urlpattern and redirect to that, that would be the most Django-ey way to do it. It's not documented (so not guaranteed to work in future Django versions) but the redirect shortcut method can take a view function, so you can almost do redirect(ClassView. as_view()) ...
You need to apply the decorator to the dispatch method of the class based view. This can be done as follows: class ProfileView(View): @youdecorator def dispatch(self,request,*args,**kwargs): return super(ProfileView,self). dispatch(request,*args,**kwargs) //Rest of your code.
Class based views are excellent if you want to implement a fully functional CRUD operations in your Django application, and the same will take little time & effort to implement using function based views.
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
Found it:
def render_to_response(self, context): if not self.videos: return redirect('other_page') return super(VideosView, self).render_to_response(context)
This is called for all HTTP methods
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With