Adding a DeleteView in Django

PythonServer Side ProgrammingProgramming

DeleteView is a view in Django which is used to delete any model data from the frontend. It is a built-in view that can be easily applied. It acts like admin page in deleting the view. It is really helpful in real-world projects.

First of all, create a Django project and an app. I created the project with the name "tutorial11" and the app with the name "modelFormsDemo".

Now, let's do some basic things. Add the app in settings.py

INSTALLED_APPS+ = ['modelFormsDemo']

In project's urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
   path('admin/', admin.site.urls),
   path('', include('modelFormsDemo.urls'))
]

Here we included the app's url.

In app's urls.py

from django.urls import path,include
from . import views

urlpatterns = [
   path('', views.home,name="home"),
   path('student/delete//', views.StudentDeleteView.
as_view(),name="delete"),
   path('success/',views.success,name='success')
]

Here we created 3 URLs: one for rendering the frontend, DeleteView for deleting, and Success for redirecting after deleting.

Example

In models.py, add this −

from django.db import models

# Create your models here.
class Student(models.Model):
   name=models.CharField(max_length=100)
   standard=models.CharField(max_length=100)
   section=models.CharField(max_length=100)

Here we created a simple model.

In views.py, add the following −

from django.shortcuts import render
from .forms import StudentForm
from django.views.generic.edit import DeleteView
from .models import Student
from django.urls import reverse_lazy

# Create your views here.
def home(request):
   if request.method=='POST':
      form=StudentForm(request.POST)
      if form.is_valid():
         form.save()
   stuForm=StudentForm()
   return render(request,'home.html',{"stu_form":stuForm})
class StudentDeleteView(DeleteView):
   model=Student
   template_name='delete_view.html'
   success_url=reverse_lazy("success")

Here, in home view, we rendered the frontend and in DeleteView, we rendered the delete_view.html which will ask for delete confirmation.

Create forms.py in app directory and write this −

from django import forms
from .models import Student

class StudentForm(forms.ModelForm):
   class Meta:
      model=Student
      fields=['name', 'standard', 'section']

Here we created our simple form which we will render in home view.

Now create a templates folder and add three files inside it home.html, delete_view.html and success.html.

In home.html

<!DOCTYPE html>
<html>
   <head>
      <title>TUT</title>
   </head>
   <body>
      {% for fm in stu_form %}
      <form method="post">
         {%csrf_token%}
         {{fm.errors}}<br>
         {{fm.label}}:{{fm}}<br>
      {%endfor%}
         <button type="submit">Submit</button>
      </form>
   </body>
</html>

In delete_view.html −

<!DOCTYPE html>
<html>
   <head>
      <title>TUT</title>
   </head>
   <body>
      <form method="post">{% csrf_token %}
         <p>Are you sure you want to delete "{{ object }}"?</p>
         <input type="submit" value="Confirm">
      </form>
   </body>
</html>

In success.html

<!DOCTYPE html>
<html>
   <head>
      <title>TUT</title>
   </head>
   <body>
      <h2>Success</h2>
   </body>
</html>


All the three are HTML files that we are rendering. home.html is for adding student, delete_view.html is for deleting student, and success.html for redirecting.

Now you can proceed to check the output.

Output

Home.html


If you go to http://127.0.0.1:8000/student/delete/(student object id)/, then you will see our delete_view.html.

Delete_view.html


raja
Published on 26-Aug-2021 12:39:12
Advertisements