0% found this document useful (0 votes)
20 views3 pages

Django Employee Delete View Tutorial

This document outlines the steps to create a delete view in Django using function-based views for the Employee model. It includes defining the model, creating the delete view function to handle GET and POST requests, designing a confirmation template, and configuring the URL mapping. Following these steps allows for the deletion of an employee after confirmation via a specified URL.

Uploaded by

yegurusiva2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Django Employee Delete View Tutorial

This document outlines the steps to create a delete view in Django using function-based views for the Employee model. It includes defining the model, creating the delete view function to handle GET and POST requests, designing a confirmation template, and configuring the URL mapping. Following these steps allows for the deletion of an employee after confirmation via a specified URL.

Uploaded by

yegurusiva2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Creating a delete view in Django using function-based views (FBVs) involves handling both

the GET request to confirm deletion and the POST request to process the deletion. Let's
create a delete view for the Employee model:

Step 1: Define the Model

Assume you have already defined the Employee model in your models.py as follows:

python
Copy code
# models.py
from django.db import models

class Employee(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
department = models.CharField(max_length=100)
salary = models.DecimalField(max_digits=10, decimal_places=2)

Step 2: Create Delete View Function (FBV)

In your views.py, create a function-based view for deleting an employee.

python
Copy code
# views.py
from django.shortcuts import render, redirect, get_object_or_404
from .models import Employee

def employee_delete_view(request, employee_id):


# Fetch the employee object from the database
employee = get_object_or_404(Employee, id=employee_id)

if request.method == 'POST':
# Delete the employee object from the database
employee.delete()

# Redirect to a success page or any other URL


return redirect('employee_list') # Adjust this to your desired URL
after deletion

# Render the template with the employee data for confirmation


return render(request, 'employee_delete.html', {'employee': employee})

Step 3: Create Delete Template (HTML)

Create a template file named employee_delete.html in your templates directory to confirm


the deletion.

html
Copy code
<!-- employee_delete.html -->
<!DOCTYPE html>
<html>
<head>
<title>Delete Employee</title>
</head>
<body>
<h1>Delete Employee</h1>
<p>Are you sure you want to delete {{ employee.first_name }} {{
employee.last_name }}?</p>
<form method="post">
{% csrf_token %}
<button type="submit">Confirm Delete</button>
</form>
</body>
</html>

Step 4: URL Configuration

Map the delete view function to a URL in your urls.py file, passing the employee ID as a
parameter.

python
Copy code
# urls.py
from django.urls import path
from .views import employee_delete_view

urlpatterns = [
path('employees/<int:employee_id>/delete/', employee_delete_view,
name='employee_delete'),
# Other URL patterns...
]

Explanation:

 Step 2 (employee_delete_view):
o This function retrieves the Employee object using get_object_or_404.
o It handles both GET and POST requests.
o In the GET request, it renders a confirmation template
(employee_delete.html) with the employee's information.
o In the POST request, it deletes the employee from the database using
employee.delete() and redirects to a specified URL (employee_list in this
example).
 Step 3 (employee_delete.html):
o This template confirms the deletion of an employee.
o It includes a CSRF token for security and a submit button ( <button
type="submit">Confirm Delete</button>).
 Step 4 (URL Configuration):
o Maps the employee_delete_view to a URL pattern
(/employees/<int:employee_id>/delete/).
o This pattern includes the employee_id parameter to identify which employee
to delete.

With these steps completed, visiting a URL like /employees/1/delete/ in your Django
application will render the delete confirmation page for the employee with ID 1. After
confirming the deletion, it will delete the employee from the database and redirect to the
specified URL (employee_list in this case). Adjust the URLs and templates as per your
application's structure and requirements.

You might also like