在模板中使用Django DeleteView,可以通过以下步骤实现:
from django.views.generic import DeleteView
from .models import Book
class BookDeleteView(DeleteView):
model = Book
template_name = 'book_delete.html'
from .views import BookDeleteView
urlpatterns = [
path('book/<int:pk>/delete/', BookDeleteView.as_view(), name='book_delete'),
]
<h1>Delete Book</h1>
<p>Are you sure you want to delete this book?</p>
<form method="post">
{% csrf_token %}
<input type="submit" value="Delete">
</form>
<h2>{{ object.title }}</h2>
<p>{{ object.author }}</p>
这样,当用户访问/book/1/delete/路径时,将会显示一个确认删除的页面,用户点击"Delete"按钮后,该书籍对象将会被删除。
推荐的腾讯云相关产品:腾讯云云服务器(CVM)和腾讯云数据库MySQL版(TencentDB for MySQL)。
领取专属 10元无门槛券
手把手带您无忧上云