In a django view, how do I redirect a user to the login view if that don't belong to the object set?

Viewed 2190

I feel like this is really simple and I'm just missing it.

I have a very simple generic class based view detail.

When I do teh get_object I want to make sure that the request.user is in the set that belongs to the object. If not, redirect them to the login.

Here's my view:

class AwesomeDetail(LoginRequiredMixin, DetailView):
    """
    An awesome detail
    """
    template_name = "awesomeness/detail.html"

    def get_object(self):
        awesomeness = ModelName.objects.get(id=self.kwargs['pk'])
        if self.request.user in awesomeness.staff.all():
            return awesomness
        else:
            return redirect('account_login')

Staff is a many to many to users. What am I missing? The redirect isn't happening. It renders the template, but of course, awesomeness is missing.

2 Answers
Related