๐ Introduction
Deleting records is a critical operation.
We should never delete directly without user confirmation.
๐ Best Practice:
- Show confirmation page
- Ask user to confirm
- 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
- User clicks “Delete”
- Confirmation page opens
- User clicks “Yes”
- POST request sent
- Record deleted
- 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
- Add popup confirmation
- Add “Deleted Successfully” message
- Soft delete (mark as inactive instead of deleting)
- Delete multiple records
- 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
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
