6👍
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)
3👍
This is purely supplemental to @Alasdair’s answer. It should really be a comment, but couldn’t be formatted properly that way. Instead of actually redefining get
on the ListView
, you could override simply with:
class MyView(ListView):
allow_empty = False # Causes 404 to be raised if queryset is empty
def get(self, request, *args, **kwargs):
try:
return super(MyView, self).get(request, *args, **kwargs)
except Http404:
return HttpResponseRedirect("/empty-queryset-url/")
That way, you’re not responsible for the entire implementation of get
. If Django changes it in the future, you’re still good to go.
- [Django]-Getting Http403 when saving files through django s3 middleware (but can save using boto in shell)
- [Django]-Django async update a single page template