[Django]-I have a TemplateDoesNotExist error. I can update my models but not delete them?

5πŸ‘

βœ…

For some reason when using UpdateView, your button in html can be just a simple link with the href pointing to the correct url which in my case pointed to ClientUpdate in my views file …

But for DeleteView the html has to be in a <form>. This was the only code i had to change to make this work. I basically put the form in place of the
<a href="{% url 'clients:client-delete' pk=client.id %}"....>

<div class="panel-footer">
  <a type="button" class="btn btn-sm btn-success"><i class="glyphicon glyphicon-envelope"></i></a>
    <span class="pull-right">
      <a href="{% url 'clients:client-update' pk=client.id %}" type="button" class="btn btn-small btn-info"><i class="glyphicon glyphicon-edit"></i></a>
      <form action="{% url 'clients:client-delete' pk=client.id %}" method="post" style="display: inline;">
          {% csrf_token %}
          <input type="hidden" name="client_id" value="{{ client.id }}"/>
               <button type="submit" class="btn btn-danger btn-small">
                   <span class="glyphicon glyphicon-trash"></span>
                </button>
        </form>
     </span>

πŸ‘€ryan pickles

1πŸ‘

By default, the DeleteView shows a confirmation page for get requests, and deletes the object for post requests. You need to create a template clients/client_confirm_delete.html (or set template_name on the view) to handle GET requests. There is an example template in the docs:

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

Your other option is to add a form to your index page, so that users submit a post request to the delete page. Note that this means that the object will be deleted immediately without confirmation. Even if you do this, it might be a good idea to add a template for GET requests, otherwise you could get errors when users/bots navigate directly to your delete url.

πŸ‘€Alasdair

0πŸ‘

u need to create a dedicated HTML page inside the CBV to execute the delete operation

Leave a comment