Django

Implement Delete Functionality with Confirmation Page

๐Ÿ“Œ Introduction

Deleting records is a critical operation.
We should never delete directly without user confirmation.

๐Ÿ‘‰ Best Practice:

  1. Show confirmation page
  2. Ask user to confirm
  3. Delete only after confirmation

In this program, we will:

  • fetch a record
  • show confirmation page
  • delete record on confirmation

๐ŸŽฏ Program Statement

๐Ÿ‘‰ Implement delete functionality with confirmation page.


๐Ÿง  Concept

This program uses:

  • get_object_or_404()
  • POST request for deletion
  • confirmation template

โš™๏ธ Step 1: Create Delete View


๐Ÿ“ File: views.py

๐Ÿ”น Path:

myproject/myapp/views.py

๐Ÿ”น Code:

from django.shortcuts import render, get_object_or_404, redirect
from .models import Student

def delete_student(request, id):
student = get_object_or_404(Student, id=id)

if request.method == 'POST':
student.delete()
return redirect('student_table')

return render(request, 'delete_student.html', {
'student': student
})

๐Ÿง  Explanation

  • Fetch record using id
  • If user confirms (POST request) โ†’ delete record
  • Redirect to list page after deletion

โš™๏ธ Step 2: URL Mapping


๐Ÿ“ File: urls.py

from django.urls import path
from myapp import views

urlpatterns = [
path('delete/<int:id>/', views.delete_student, name='delete_student'),
]

โš™๏ธ Step 3: Create Confirmation Template


๐Ÿ“ File: delete_student.html

๐Ÿ”น Path:

myproject/templates/delete_student.html

๐Ÿ”น Code:

<!DOCTYPE html>
<html>
<head>
<title>Delete Student</title>
</head>
<body>

<h1>Delete Confirmation</h1>

<p>Are you sure you want to delete this record?</p>

<p><strong>Name:</strong> {{ student.name }}</p>
<p><strong>Email:</strong> {{ student.email }}</p>

<form method="post">
{% csrf_token %}
<button type="submit">Yes, Delete</button>
</form>

<br>

<a href="/student-table/">Cancel</a>

</body>
</html>

๐Ÿง  Explanation

  • Displays student details before deletion
  • POST form confirms deletion
  • Cancel link prevents accidental deletion

โš™๏ธ Step 4: Add Delete Link in Table


๐Ÿ“ File: student_table.html

Add:

<td>
<a href="/delete/{{ s.id }}/">Delete</a>
</td>

๐Ÿ”น Updated Row Example:

<tr>
<td>{{ s.id }}</td>
<td>{{ s.name }}</td>
<td>{{ s.age }}</td>
<td>{{ s.course }}</td>
<td>{{ s.email }}</td>
<td><a href="/update/{{ s.id }}/">Edit</a></td>
<td><a href="/delete/{{ s.id }}/">Delete</a></td>
</tr>

โš™๏ธ Step 5: Run Server

python manage.py runserver

๐ŸŒ Step 6: Output

๐Ÿ‘‰ Open:

http://127.0.0.1:8000/delete/1/

โœ… Confirmation Page:

Delete Confirmation

Are you sure you want to delete this record?

Name: Ritu
Email: ritu@example.com

[Yes, Delete]   [Cancel]

โœ… After Clicking Delete:

  • Record is removed
  • Redirects to student table

๐Ÿง  How It Works

  1. User clicks “Delete”
  2. Confirmation page opens
  3. User clicks “Yes”
  4. POST request sent
  5. Record deleted
  6. Redirect to list page

๐Ÿ”ฅ Key Concepts


Delete Record

student.delete()

Safe Fetch

get_object_or_404(Student, id=id)

Redirect

return redirect('student_table')

POST Method

<form method="post">

Ensures safe deletion.


โš ๏ธ Common Errors


โŒ Deleting without confirmation

๐Ÿ‘‰ Dangerous practice


โŒ Using GET for delete

Never do:

if request.method == 'GET':

โŒ Forgot CSRF token

{% csrf_token %}

โŒ Redirect error

Ensure URL name exists:

'student_table'

โŒ Record not found

Handled using:

get_object_or_404()

๐Ÿงช Practice Questions

  1. Add popup confirmation
  2. Add “Deleted Successfully” message
  3. Soft delete (mark as inactive instead of deleting)
  4. Delete multiple records
  5. Add undo option

๐ŸŽค Viva Questions & Answers


1. What is delete operation in Django?

Delete operation removes a record permanently from the database.


2. Why do we use confirmation page?

To prevent accidental deletion of important data.


3. What is delete() method?

It removes the object from the database.


4. Why is POST method used for deletion?

Because deletion is a sensitive operation and should not be triggered by URL directly.


5. What is redirect()?

It redirects the user to another page after an action.


6. What is get_object_or_404()?

It fetches the object or shows a 404 error if not found.


7. What happens after deletion?

The record is permanently removed from database.


8. Can deleted data be recovered?

No, unless backup or soft delete is implemented.


9. What is soft delete?

Instead of deleting, we mark record as inactive.


10. Why is this program important?

It completes CRUD operations, which are the backbone of all web applications.


๐Ÿ‘‰ Next Post: Implement Search Functionality using icontains
๐Ÿ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *