django 尽管有规定的代码,但网站不会删除元素

vq8itlhq  于 2023-01-18  发布在  Go
关注(0)|答案(1)|浏览(96)

我有一个代码,可以添加、修改、删除数据,添加、修改都很好用,但是删除就不行了,简直就是按键都没有React,到底是什么原因呢?
views.py:

from django.shortcuts import render
from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView
from .models import Post
from .forms import PostForm, EditForm
from django.urls import reverse_lazy

class DeletePostView(UpdateView):
    model = Post
    template_name = 'delete_post.html'
    fields = '__all__'
    sucсess_url=reverse_lazy('home')

urls.py

from django.urls import path
from .views import HomeView, ArticleDetailView, AddPostView, UpdatePostView, DeletePostView
urlpatterns = [
    #path('', views.home, name="home"),
    path('', HomeView.as_view(), name="home"),
    path('article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
    path('add_post/', AddPostView.as_view(), name='add_post'),
    path('article/edit/<int:pk>', UpdatePostView.as_view(), name='update_post'),
    path('article/<int:pk>/remove', DeletePostView.as_view(), name='delete_post'),
]

delete_post.html

{% extends 'base.html' %}
{% block content %}
<head>
    <title>DELETE</title>
  </head>
<h3>Delete: {{post.title}}</h3>
<div class="form-group">
<form method="POST">
   {% csrf_token %}
   <button class="btn btn-secondary">Delete</button>
   </form>
{% endblock %}
snz8szmq

snz8szmq1#

尝试将这个helper方法作为操作添加到表单元素中,并将按钮类型设置为submit。
action="{% url 'delete_post' Post.id %}"
就像这样:

{% extends 'base.html' %}
{% block content %}
<head>
    <title>DELETE</title>
  </head>
<h3>Delete: {{post.title}}</h3>
<div class="form-group">
<form method="POST" action="{% url 'delete_post' Post.id %}">
   {% csrf_token %}
   <button class="btn btn-secondary" type="submit">Delete</button>
   </form>
{% endblock %}

编辑:
刚刚发现您将UpdateView用于您的DeleteView。
将其编辑为:

class DeletePostView(DeleteView):
    model = Post
    template_name = 'delete_post.html'
    fields = '__all__'
    sucсess_url=reverse_lazy('home')

相关问题