I have a code that can add, change or delete data. Adding and changing works well, but deleting doesn't work. There is simply no response to pressing the button. What is the reason?
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 >
<form method="POST">
{% csrf_token %}
<button >Delete</button>
</form>
{% endblock %}
CodePudding user response:
Try adding this helper method as an action on your form element and type of the button as submit.
action="{% url 'delete_post' Post.id %}"
Like this:
{% extends 'base.html' %}
{% block content %}
<head>
<title>DELETE</title>
</head>
<h3>Delete: {{post.title}}</h3>
<div >
<form method="POST" action="{% url 'delete_post' Post.id %}">
{% csrf_token %}
<button type="submit">Delete</button>
</form>
{% endblock %}
EDIT:
Just spotted that you use the UpdateView for your DeleteView.
Edit it to:
class DeletePostView(DeleteView):
model = Post
template_name = 'delete_post.html'
fields = '__all__'
sucсess_url=reverse_lazy('home')