Open In App

DeleteView - Class Based Views Django

Last Updated : 19 Nov, 2025
Comments
Improve
Suggest changes
10 Likes
Like
Report

A DeleteView is a built-in class-based view used to remove a record from the database. It automatically fetches the record, shows a confirmation page, deletes the record, and redirects.

  • Specify the model that contains the record to be deleted.
  • Provide a template to show the delete confirmation page.
  • Set a success URL to redirect after the record is deleted.

Example: Consider a project named 'geeksforgeeks' having an app named 'geeks'.  After you have a project and an app, let's create a model of which we will be creating instances through our view.

In geeks/models.py:

Python
from django.db import models
 
# declare a new model with a name "GeeksModel"
class GeeksModel(models.Model):

    # fields of the model
    title = models.CharField(max_length = 200)
    description = models.TextField()

    # renames the instances of the model
    # with their title name
    def __str__(self):
        return self.title

After creating this model, we need to run two commands in order to create Database for the same:

Python manage.py makemigrations
Python manage.py migrate

Let's create some instances of this model using shell, enter the following command to launch Python shell:

Python manage.py shell

Enter following commands to create entries in database:

>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create( title="title1", description="description1")
>>> GeeksModel.objects.create(title="title2", description="description2")
>>> GeeksModel.objects.create(title="title3", description="description3")

Now we have everything ready for back end. Verify that instances have been created from http://localhost:8000/admin/geeks/geeksmodel/ 

django-listview-check-models-instancesClass Based Views automatically setup everything. One just needs to specify which model to create DeleteView for, then Class based DeleteView will automatically try to find a template in app_name/modelname_confirm_delete.html. In our case it is geeks/templates/geeks/geeksmodel_confirm_delete.html.

Create class based view in geeks/views.py:

Python
from django.views.generic.edit import DeleteView

# Relative import of GeeksModel
from .models import GeeksModel

class GeeksDeleteView(DeleteView):
    # specify the model you want to use
    model = GeeksModel
    
    # can specify success url
    # url to redirect after successfully
    # deleting object
    success_url ="/"
    
    template_name = "geeks/geeksmodel_confirm_delete.html"

Now create a url path to map the view. In geeks/urls.py: 

Python
from django.urls import path

# importing views from views..py
from .views import GeeksDeleteView
urlpatterns = [
    # <pk> is identification for id field,
    # slug can also be used
    path('<pk>/delete/', GeeksDeleteView.as_view()),
]

Create a template in templates/geeks/geeksmodel_confirm_delete.html:

html
<form method="post">{% csrf_token %}
    
<p>Are you sure you want to delete "{{ object }}"?</p>

    <input type="submit" value="Confirm">
</form>

Check what is there on: http://localhost:8000/1/delete 
 

django-deleteview-class-based-views

Tap confirm and object will redirect to "success_url" defined in the view. Let's check if title1 is deleted from database. 

django-deleteview-sucess

Suggested Quiz
3 Questions

What is the main purpose of DeleteView in Django?

  • A

    To display a list of objects

  • B

    To create a new object

  • C

    To remove an existing record from the database

  • D

    To update an existing record

Explanation:

DeleteView handles deletion of a model instance from the database after confirmation.

What does DeleteView do when it receives a GET request by default?

  • A

    Immediately deletes the object

  • B

    Redirects to home page

  • C

    Displays a confirmation page before deletion

  • D

    Shows an error

Explanation:

On GET request DeleteView shows a confirmation template asking user to confirm before deleting.

After a successful deletion by DeleteView what must be provided so that user is redirected properly?

  • A

    A success message only

  • B

    A new database record

  • C

    A list of all objects

  • D

    A success_url specifying where to redirect

Explanation:

DeleteView requires a success_url attribute so that after deletion the response redirects to a given URL.

Quiz Completed Successfully
Your Score :   2/3
Accuracy :  0%
Login to View Explanation
1/3 1/3 < Previous Next >

Explore