1

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?

shaan
  • 351
  • 2
  • 15
  • You should use [`render()`](https://docs.djangoproject.com/en/3.1/topics/http/shortcuts/#render) function – JPG Dec 02 '20 at 13:57
  • @ArakkalAbu - Thanks. Able to see message now. But, The **object details** (of the object that is being deleted) goes missing from the page now. **For example**, if the object is **Lower Manhattan**, its details are available when the `Delete` option is exercised first (done to facilitate ease of user comprehension). Now when the app encounters the `ProtectedError`, these details go missing altogether. Possibly because on exception, the context is not being transmitted as in normal cases (i.e. in the absence of an **error**). What should I do to keep the context data on the page at all times? – shaan Dec 02 '20 at 14:37

1 Answers1

1

First, change your delete(...) method to catch the ProtectedError error (which you have already done) and then pass the error message as context data to the template as

class TerritoryDeleteView(LoginRequiredMixin, DeleteView):
    template_name = ".../trty_delete.html"
    model = Territory
    success_url = reverse_lazy('territories_list')

    def delete(self, request, *args, **kwargs):
        try:
            return super().delete(request, *args, **kwargs)
        except ProtectedError:
            self.object = self.get_object()
            context = self.get_context_data(
                object=self.object,
                error="Any error msg"
            )
            return self.render_to_response(context)

But, this is not enough, we have to update the template file too.

<form method="post">{% csrf_token %}
    {% if error %}
        {{ error }}
    {% else %}
        <p>Are you sure you want to delete "{{ object }}"?</p>
        <input type="submit" value="Confirm">
    {% endif %}

</form>

Note: This may not fit exactly in your case, but, probably this will give you an idea of how to manage the error message nicely.

JPG
  • 82,442
  • 19
  • 127
  • 206
  • I had already taken care of the conditional display of the **error message**. The part that I was sorely missing was the details of the **object being deleted**. Grateful for your answer. Saved my day!! – shaan Dec 02 '20 at 15:15