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

相关推荐
weixin_BYSJ19871 小时前
springboot酒店管理系统--附源码27436
java·spring boot·python·随机森林·贪心算法·eclipse·django
vx-程序开发8 小时前
django医院预约挂号系统---附源码23353
java·javascript·spring boot·python·eclipse·django·php
weixin_BYSJ198712 小时前
springboot医疗信息管理系统---附源码17465
java·javascript·spring boot·python·django·flask·php
vx-程序开发1 天前
springboot农产品运输服务平台---附源码75498
java·javascript·spring boot·python·eclipse·django·php
John jj1 天前
拆解 Telegram 群组频道收录市场:三类方案,一个可运行的评分模型,目前TG中文人工评分加模型评分机制——LetsTG收录“快速”、“无门槛”
大数据·后端·python·深度学习·搜索引擎·django·全文检索
2401_868534781 天前
论信息系统安全保障规划与设计
python·django
Python私教1 天前
如意 Django CRM 容器化实战:后端、前端、数据库、Redis 的协同启动逻辑
前端·数据库·django
weixin_BYSJ19871 天前
【java项目分享】springboot阅读推荐平台10600
java·javascript·spring boot·python·django·flask·php
小王同学66664 天前
Django 5 中实现文件上传功能
数据库·django·sqlite
vx-程序开发4 天前
【java项目分享】springboot农产品销售平台14952
java·javascript·spring boot·python·eclipse·django·php