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,访问页面

相关推荐
大模型丫丫1 天前
工业级 RAG 为什么需要 Elasticsearch?
elasticsearch·django·jenkins
Mr数据杨1 天前
【Codex】接入音频转录服务实现课堂语音结构化处理
django·音视频·codex·项目开发
Mr数据杨3 天前
【Codex】用学生考试初始化模块批量准备考试基础数据
django·codex·项目开发
典典分享指南3 天前
Excel SUMIFS 跨表汇总实战:多表条件求和的完整指南
django·flask·numpy·pyqt·fastapi
BYSJMG3 天前
计算机毕设选题|基于大数据与文本挖掘的自动驾驶事故成因分析及可视化研究|Hadoop+PySpark+Django+Vue
大数据·hadoop·信息可视化·django·自动驾驶·kmeans·课程设计
Mr数据杨4 天前
【Codex】用下载中心模块管理系统导出文件与下载任务
django·codex·项目开发
171320330计算机毕设编程4 天前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
青 春 记 忆4 天前
零基础入门python43:Django接口测试与库存边界
python·django·后端开发
Mr数据杨4 天前
【Codex】用智能助手问题反馈模块收集AI使用问题
人工智能·django·codex·项目开发
ikun_文5 天前
Django框架路由Router的使用
python·pycharm·django