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')
] ที่นี่เราสร้าง URL 3 รายการ:รายการหนึ่งสำหรับแสดงผลส่วนหน้า, DeleteView สำหรับการลบ และสำเร็จสำหรับการเปลี่ยนเส้นทางหลังจากลบ
ตัวอย่าง
ใน models.py , เพิ่มสิ่งนี้ −
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)
เราได้สร้างแบบจำลองที่เรียบง่าย
ใน 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
# 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") ที่นี่ ในโฮมวิว เราแสดงฟรอนต์เอนด์ และใน 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']
ที่นี่เราสร้างฟอร์มอย่างง่ายของเราซึ่งเราจะแสดงผลในโฮมวิว
ตอนนี้สร้าง เทมเพลต และเพิ่มไฟล์สามไฟล์เข้าไป 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 −

หากคุณไปที่ http://127.0.0.1:8000/student/delete/(student object id)/ คุณจะเห็น delete_view.html ของเรา
Delete_view.html −
