DeleteView Django में एक व्यू है जिसका उपयोग फ्रंटएंड से किसी भी मॉडल डेटा को डिलीट करने के लिए किया जाता है। यह एक बिल्ट-इन व्यू है जिसे आसानी से लागू किया जा सकता है। यह व्यू को डिलीट करने में एडमिन पेज की तरह काम करता है। यह वास्तविक-विश्व प्रोजेक्ट्स में वास्तव में उपयोगी है।
सबसे पहले, एक Django प्रोजेक्ट और एक ऐप बनाएं। मैंने प्रोजेक्ट को "tutorial11" नाम से और ऐप को "modelFormsDemo" नाम से बनाया।
अब, कुछ बुनियादी चीजें करते हैं। settings.py में ऐप जोड़ें:
INSTALLED_APPS += ['modelFormsDemo']
प्रोजेक्ट के urls.py में:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('modelFormsDemo.urls'))
]
यहां हमने ऐप का url शामिल किया।
ऐप के 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')
]
यहां हमने 3 URLs बनाए: एक फ्रंटएंड रेंडर करने के लिए, डिलीट के लिए DeleteView, और डिलीट के बाद रीडायरेक्ट के लिए Success।
उदाहरण
models.py में, यह जोड़ें:
from django.db import models
class Student(models.Model):
name = models.CharField(max_length=100)
standard = models.CharField(max_length=100)
section = models.CharField(max_length=100)
यहां हमने एक साधारण मॉडल बनाया।
views.py में, निम्नलिखित जोड़ें:
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
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")
यहां, होम व्यू में, हमने फ्रंटएंड रेंडर किया और DeleteView में, हमने delete_view.html रेंडर किया जो डिलीट कन्फर्मेशन मांगेगा।
ऐप डायरेक्टरी में forms.py बनाएं और यह लिखें:
from django import forms
from .models import Student
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ['name', 'standard', 'section']
यहां हमने अपना साधारण फॉर्म बनाया जिसे हम होम व्यू में रेंडर करेंगे।
अब एक templates फोल्डर बनाएं और इसमें तीन फाइलें जोड़ें: home.html, delete_view.html और success.html।
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>
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>
success.html में:
<!DOCTYPE html>
<html>
<head>
<title>TUT</title>
</head>
<body>
<h2>Success</h2>
</body>
</html>
ये तीनों HTML फाइलें हैं जिन्हें हम रेंडर कर रहे हैं। home.html छात्र जोड़ने के लिए है, delete_view.html छात्र डिलीट करने के लिए, और success.html रीडायरेक्ट के लिए।
अब आप आउटपुट जांचने के लिए आगे बढ़ सकते हैं।
आउटपुट
Home.html -

यदि आप https://127.0.0.1:8000/student/delete/(student object id)/ पर जाते हैं, तो आप हमारा delete_view.html देखेंगे।
Delete_view.html -
