← Back to Django Course | Chapter 9: Class-Based Views | Lesson 7 of 10

DeleteView Basics

DeleteView is a ready-made confirmation page that removes a row from the database once you confirm you really want to delete it.

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.

markup
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.

markup
<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.

markup
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. #}
Common Mistakes
  1. Forgetting to add a confirmation template, and being surprised the delete does not happen on a simple GET request.
  2. Not setting success_url, causing Django to raise an error after a successful deletion because it does not know where to redirect.
  3. Wiring the delete link as a plain GET link instead of a POST form, which breaks the intended confirm-then-POST flow.
Chapter Summary
  • 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.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.