I am trying to use Django generic DeleteView using the confirmation page. The setup is working as intended.
Later the business logic was changed to prevent deletion if there are child instances assigned to the object being deleted, using on_delete=models.PROTECT
in the model.
And the DeleteView
has been modified to the following:
class TerritoryDeleteView(LoginRequiredMixin, DeleteView):
template_name = ".../trty_delete.html"
model = Territory
success_url = reverse_lazy('territories_list')
# THE FOLLOWING MODIFICATION DONE:
def delete(self, request, *args, **kwargs):
self.object = self.get_object()
try:
self.object.delete()
return HttpResponseRedirect(success_url)
except ProtectedError:
error_message = "This object can't be deleted, as an Outlet is already assigned to the Territory..."
return JsonResponse(error_message, safe=False)
The above (modified) view works fine. However, in case it encounters the ProtectedError
, the error_message
is shown in a blank browser page.
How could I display the error_message
in the confirmation page (template) itself?