Delete View - Function based Views Django
A Delete View is used to remove a record from the database. It is useful when users need to delete items like blog posts, products, or profiles. When using function-based Delete Views, we can:
- Fetch the record to delete from the database using its ID (or another unique field).
- Display a confirmation page (optional but recommended).
- Delete the record if confirmed.
- Redirect to another page after deletion.
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:
# import the standard Django Model
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
Now let's create some instances of this model using shell, run form bash,
Python manage.py shell
Enter following commands
>>> from geeks.models import GeeksModel
>>> GeeksModel.objects.create(
title="title1",
description="description1").save()
>>> GeeksModel.objects.create(
title="title2",
description="description2").save()
>>> GeeksModel.objects.create(
title="title2",
description="description2").save()
Now we have everything ready for back end. Verify that instances have been created from http://localhost:8000/admin/geeks/geeksmodel/

Now let's create our delete view first, In geeks/views.py:
from django.shortcuts import (get_object_or_404,
render,
HttpResponseRedirect)
from .models import GeeksModel
# delete view for details
def delete_view(request, id):
# dictionary for initial data with
# field names as keys
context ={}
# fetch the object related to passed id
obj = get_object_or_404(GeeksModel, id = id)
if request.method =="POST":
# delete object
obj.delete()
# after deleting redirect to
# home page
return HttpResponseRedirect("/")
return render(request, "delete_view.html", context)
Now a url mapping to this view with a regular expression of id, In geeks/urls.py:
from django.urls import path
# importing views from views..py
from .views import delete_view
urlpatterns = [
path('<id>/delete', delete_view ),
]
Template for delete view includes a simple form confirming whether user wants to delete the instance or not. In geeks/templates/delete_view.html:
<div class="main">
<!-- Create a Form -->
<form method="POST">
<!-- Security token by Django -->
{% csrf_token %}
Are you want to delete this item ?
<input type="submit" value="Yes" />
<a href="/">Cancel </a>
</form>
</div>
Everything ready, now let's check if it is working or not, visit http://localhost:8000/2/delete

Let's check if instance has been deleted or not:

Now, we can implement this view as required using obj.delete() function.
What is the main purpose of a Delete View in Django function based views?
-
A
To display a list of records
-
B
To permanently remove a specific record from the database
-
C
To update an existing record
-
D
To create a new record
A Delete View is used to locate a target object and remove it from the database.
In a typical Delete View what should happen when a GET request is received before deletion?
-
A
Immediately delete the object
-
B
Redirect to a list view directly
-
C
Raise an error
-
D
Show a confirmation page that asks for a deletion confirmation
A confirmation page is usually shown so deletion occurs only after user approval.
After a user confirms deletion through a POST request in a Delete View what is the expected behavior?
-
A
Delete the object and redirect to a success page or another view
-
B
Ignore the deletion request if the user is anonymous
-
C
Update the object instead of deleting
-
D
Delete the object and then recreate a new one
After confirmation, the object is deleted and the view redirects to another page such as a list view.