在 Django 中添加 DeleteView

pythonserver side programmingprogramming更新于 2025/10/1 8:44:17

DeleteView 是 Django 中的一个视图,用于从前端删除任何模型数据。它是一个可以轻松应用的内置视图。它在删除视图时就像管理页面一样。它在实际项目中非常有用。

首先,创建一个 Django 项目和一个应用程序。我创建了名为 "tutorial11" 的项目和名为 "modelFormsDemo" 的应用程序。

现在,让我们做一些基本的事情。在 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'))
]

这里我们包含了应用程序的 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 个 URL:一个用于呈现前端,一个用于删除的 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 −


如果您访问 http://127.0.0.1:8000/student/delete/(student object id)/,那么您将看到我们的 delete_view.html。

Delete_view.html −



相关文章


有用资源