The object is retrieved inside the dispatch () method, so your decorator cannot use it. You can check membership in the overriden get () method:
class ProjectDetailView(DetailView): context_object_name = "project" model = Project def get(self, request, **kwargs): self.object = self.get_object() if not self.object.is_member(self.request.user): return HttpResponseRedirect('/')
If you want to stick with the decorator, you will need to restore the object from the database in your decorator based on the arguments (id or slug) to view. But you will retrieve the object from the database twice, first in your decorator, and then in the view.
source share