Django - how to delete an object using a view
You need to have your delete
function(btw, name it something else, like delete_person
) take in an argument, pk.
def delete_person(request, pk):
...
...
Then in your urlconf, do something like this
url(r'^delete_person/(?P<pk>\d+)/$', 'delete_person', name='delete_person')
And then generate the url in the template like so
{% url 'delete-person' person.id %}
PS - No need to have your urls end with .html
PPS - Would be a good idea to do some validation in the view to make sure that the user is authorized to delete the person.
I think you could use a link instead of form:
Replace this line
<td>
<form action="/delete.html">
<input type="submit" value="Delete">
</form>
</td>
with this
<td><a href="/delete/{{ person.id }}">Delete</a></td>
In urls.py you should add the following line to relate your view with url:
url(r'^delete/(?P<person_pk>.*)$', 'person.views.delete' name='delete-person'),
Then change your view:
def delete(request, person_pk):
query = People.objects.get(pk=person_pk)
query.delete()
return HttpResponse("Deleted!")