How can I use Django DeleteView in my template

Viewed 412

I am using Django DeleteView in a template and I've created a url & view. But it shows "Generic detail view EmployeeDeleteView must be called with either an object pk or a slug in the URLconf." I was looking on django docs, and looks like that DeleteViews are coupled with the models. How can I use Django DeleteView here. If any one help me please.

views.py

class EmployeeDeleteView(DeleteView):
    model = Employee
    success_url = 'create_employee'
    
    def get(self, request, *args, **kwargs):
        return self.post(request, *args, **kwargs)

urls.py

path('delete_employee/<int:id>', EmployeeDeleteView.as_view(), name='delete_employee'),

Delete Button

<button class="deletebtn show-form-delete" type="submit">
                                    <i class="fa fa-trash-o"></i>&nbsp;<a
                                        onclick="return confirm('Are you sure do you want to delete {{i.name}}')"
                                        href="{% url 'delete_employee' i.id %}">Delete</a></button>
3 Answers
<form method="POST" action="{% url 'delete_employee' i.id %}">
{% csrf_token %}
<button class="deletebtn show-form-delete" type="submit" value="DELETE" onclick="return confirm('Are you sure do you want to delete {{i.name}}')">
    <i class="fa fa-trash-o"></i>
    Delete</button>
</form>

I just had the same problem! The Generic view will look for an 'int' valued named 'pk' to delete.

Therefore:

path('delete_employee/<int:pk>', EmployeeDeleteView.as_view(), name='delete_employee'),

should fix your problem!

Related