from django.views.generic import DeleteView from django.http import HttpResponseRedirect from django.contrib import messages class DeleteCheckView(DeleteView): """ Check if object actually can be deleted. Provide reasons to template if not. """ def delete(self, request, *args, **kwargs): self.object = self.get_object() success_url = self.get_success_url() reasons = self.object.getNoDeleteReasons() if reasons: # do not delete, do what get does... return self.get(request, *args, **kwargs) else: self.object.delete() messages.info(request, "Object %s has been deleted" % self.object.handle) return HttpResponseRedirect(success_url) def get_context_data(self, **kwargs): if 'reasons' not in kwargs: kwargs['reasons'] = self.object.getNoDeleteReasons() return super(DeleteCheckView, self).get_context_data(**kwargs)