DeleteView Basics
A Minimal DeleteView
DeleteView needs model and success_url; visiting its URL with GET shows a confirmation page, and submitting that page's form with POST actually deletes the object.
Example: A Minimal DeleteView
GET requests to BookDeleteView show a confirmation template; POST requests actually delete the Book.
from django.views.generic.edit import DeleteView
from django.urls import reverse_lazy
from .models import Book
class BookDeleteView(DeleteView):
model = Book
success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
The Confirmation Template
The confirmation template must contain a form that POSTs back to the same URL, including a CSRF token, since GET alone never deletes anything.
Warning: A DeleteView with no confirmation form field at all would leave users with no way to actually trigger the deletion.
Example: The Confirmation Template
The object variable is the Book about to be deleted; the form's POST is what actually triggers the deletion.
<p>Are you sure you want to delete "{{ object.title }}"?</p>
<form method="post">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Redirecting After Deletion
success_url tells DeleteView where to send the browser once the object has been removed from the database, typically back to a list page.
Example: Redirecting After Deletion
reverse_lazy("book-list") is resolved after deletion completes, sending the user back to the book list page.
from django.urls import reverse_lazy
from django.views.generic.edit import DeleteView
from .models import Book
class BookDeleteView(DeleteView):
model = Book
success_url = reverse_lazy('book-list')
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- Forgetting to add a confirmation template, and being surprised the delete does not happen on a simple GET request.
- Not setting success_url, causing Django to raise an error after a successful deletion because it does not know where to redirect.
- Wiring the delete link as a plain GET link instead of a POST form, which breaks the intended confirm-then-POST flow.
- DeleteView shows a confirmation page on GET and only deletes the object on POST.
- template_name defaults to <app>/<model>_confirm_delete.html.
- success_url must be set (or get_success_url overridden) so Django knows where to redirect after deletion.
- The confirmation template needs a <form method="post"> with a CSRF token to trigger the actual delete.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: