Django DeleteView视图

Django 的 DeleteView 是一个基于类的视图,用于处理对象的删除操作。

1,添加视图函数

Test/app3/views.py

复制代码
from django.shortcuts import render

# Create your views here.
from .models import Book

from django.views.generic import ListView
class BookListView(ListView):
    model = Book
    context_object_name = 'books'
    template_name = 'books/book_list.html'
    paginate_by = 10 # 设置展示页数数据


from django.views.generic import DetailView
class BookDetailView(DetailView):
    model = Book
    context_object_name = 'book'
    template_name = 'books/book_detail.html'


from django.views.generic.edit import CreateView
class BookCreateView(CreateView):
    model = Book
    template_name = 'books/book_form.html'
    fields = ['title', 'author', 'publication_date']
    success_url = '/app3/books/' # 重定向至书本列表路由地址

from django.urls import reverse_lazy
from django.views.generic.edit import UpdateView
class BookUpdateView(UpdateView):
    model = Book
    fields = ['title', 'author', 'publication_date']
    template_name = 'books/book_edit.html'
    success_url = reverse_lazy('book_list')


from django.shortcuts import get_object_or_404
from django.urls import reverse
from django.views.generic.edit import DeleteView
class BookDeleteView(DeleteView):
    model = Book
    template_name = 'books/book_delete.html'

    def get_object(self):
        id_ = self.kwargs.get("id")
        return get_object_or_404(Book, id=id_)

    def get_success_url(self):
        return reverse('book_list')

2,添加路由地址

Test/app3/urls.py

复制代码
from django.urls import path
from . import views

from .views import BookListView
from .views import BookDetailView
from .views import BookCreateView
from .views import BookUpdateView
from .views import BookDeleteView


urlpatterns = [
    path('books/', BookListView.as_view(), name='book_list'),
    path('books/<int:pk>/', BookDetailView.as_view(), name='book_detail'),
    path('books/new/', BookCreateView.as_view(), name='book_new'),
    path('books/<int:pk>/edit/', BookUpdateView.as_view(), name='BookUpdateView'),
    path('books/delete/<int:id>/', BookDeleteView.as_view(), name='book_delete'),
]

3,添加html代码

Test/templates/books/book_delete.html

复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<form method="POST">
    {% csrf_token %}
    <p>你确定要删除这本书吗?</p>
    <button type="submit">确认删除</button>
</form>

</body>
</html>

4,访问页面

相关推荐
IT森林里的程序猿3 小时前
基于机器学习方法的网球比赛胜负趋势预测
python·机器学习·django
kobe_OKOK_4 小时前
Django ORM 字段查询表达式(Field lookup expressions)
后端·python·django
kobe_OKOK_5 小时前
Django ORM 无法通过 `ForeignKey` 自动关联,而是需要 **根据父模型中的某个字段(比如 ID)去查询子模型**。
后端·python·django
濑户川10 小时前
深入理解Django 视图与 URL 路由:从基础到实战
后端·python·django
江上月51315 小时前
django与vue3的对接流程详解(下)
后端·python·django
万粉变现经纪人1 天前
如何解决 pip install -r requirements.txt 约束文件 constraints.txt 仅允许固定版本(未锁定报错)问题
开发语言·python·r语言·django·beautifulsoup·pandas·pip
蓑笠翁0012 天前
Django REST Framework 全面指南:从模型到完整API接口开发
后端·python·django
B站_计算机毕业设计之家2 天前
python汽车数据分析可视化系统 爬虫 懂车帝 毕业设计 Django框架 vue框架 大数据✅
爬虫·python·数据分析·django·汽车·推荐算法·懂车帝
yzx9910133 天前
数据库完整指南:从基础到 Django 集成
数据库·python·django
蓑笠翁0013 天前
从零开始学习Python Django:从环境搭建到第一个 Web 应用
python·学习·django