'I have a TemplateDoesNotExist error. I can update my models but not delete them?

I'm making my first Django site that has one app so far named clients and i keep my templates in project_name/clients/templates/clients.

Like the title says i'm able to update an instance of my model Client using UpdateView in my views file and I thought I would be able to delete an instance in the same way using DeleteView but I get the error stated previously. Any help would be appreciated. I've seen other similar posts but nothing that helped me solve this problem

Here's my url file:

from django.conf.urls import url
from . import views
app_name = 'clients'

url(r'^$', views.IndexView.as_view(), name='index'),

# /clients/11/... could be any number
url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),

# /clients/viewed/
url(r'^viewed/', views.viewed, name='viewed'),

# /clients/add/
url(r'^add/$', views.ClientCreate.as_view(), name='client-add'),

# /clients/3/update/
url(r'^(?P<pk>[0-9]+)/update$', views.ClientUpdate.as_view(), name='client-update'),

# /clients/8/delete/
url(r'^(?P<pk>[0-9]+)/delete/$', views.ClientDelete.as_view(), name='client-delete'),

]

Here's the relevant classes in my views.py:

from __future__ import unicode_literals
from django.views import generic
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.core.urlresolvers import reverse_lazy
from django.shortcuts import render
from .models import Client


class ClientUpdate(UpdateView):
    model = Client
    fields = ['name', 'age', 'height', 'weight', 
'history_of_head_trauma', 'profession', 'is_athlete', 'email']
    success_url = reverse_lazy('clients:index')


class ClientDelete(DeleteView):
    model = Client
    success_url = reverse_lazy('clients:index')

Here's the div in my index.html that holds both buttons for updating and deleting:

 <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>
                 <a href="{% url 'clients:client-delete' pk=client.id %}" type="button" class="btn btn-small btn-danger"><i class="glyphicon glyphicon-remove"></i></a>

            </span>
 </div>


Solution 1:[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.

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Vigneshwar Selva