Converting GET Actions to POST

# Link in the Template
<a href={% url 'a_url' %}?delete={{ item.pk }}>Delete Me!</a>
# Code in View
if 'delete' in request.GET:
item_pk = request.GET.get('delete')
item = get_object_or_404(models.Item, pk=item_pk)
item.delete()
return redirect(reverse('a_url'))
# We've got a button in a form now
<form method="POST">
{% csrf_token %}
<button type="submit" name="delete" value="delete">Delete Me!</button>
</form>
# Code in View
if 'delete' in request.POST:
item_pk = request.POST.get('delete')
item = get_object_or_404(models.Item, pk=item_pk)
item.delete()
return redirect(reverse('a_url'))

 by the author.

Pub Tech developer for Birkbeck CTP

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Andy Byers

Andy Byers

Pub Tech developer for Birkbeck CTP

More from Medium

React with Django TDD-2

Markdown2 + Django

Developing Django application using Docker Container

Why We Use Django Framework & How It Can Benefit Your Business