DeleteView在django上点击submit会导致405错误



我有这个小博客帖子应用程序,除了删除视图外一切都很好。一按删除键,我就转到了删除页面但是当我从DeleteView html页面点击确认时我得到了405错误

我的views.py为删除看起来像这样:

class DeletePost(DetailView):
model = Articles
template_name = 'delete_article.html'
success_url = reverse_lazy('list_articles')
我的html文件中提到的链接是这样的:
<!DOCTYPE html>
{% extends 'base.html' %}
{% block body %}
<div class="container">
{% for i in articles %}
<div class="jumbotron">
<h2>{{i.title}}</h2>
<p>- {{i.author}}</p>
{{i.created_on}}
{{i.updated_on}}
<p>{{i.content}}</p>
<a class="btn btn-warning" href="{% url 'update_article' i.id %}">Update</a>
<a class="btn btn-danger" href="{% url 'delete_article' i.id %}">Delete</a>
</div>
{% endfor %}
</div>
{% endblock %}   

url .py是这样的:

from django.urls import path
from mysite import views
urlpatterns = [
path('articles/', views.PostList.as_view(), name='list_articles'),
path('articles/<pk>/', views.PostDetail.as_view(), name='detail_article'),
path('create/new/', views.CreateNewPost.as_view(), name='create_new_article'),
path('update/<pk>/', views.UpdatePost.as_view(), name='update_article'),
path('delete/<pk>/', views.DeletePost.as_view(), name='delete_article'),
]

和html for DeleteView:

<!DOCTYPE html>
{% extends 'base.html' %}
{% block body %}
<form method="post">
{% csrf_token %}
<p>Are you sure you want to delete "{{ object }}"?</p>
{{ form.as_p }}
<input class='btn btn-danger' type="submit"  value="Confirm">
</form>
{% endblock %}

您提到DetailView要删除,这是不正确的,请提及DeleteView代替from django.views.generic.edit import DeleteView

Note:基于类的视图需要实际视图名称作为后缀,因此您应该将其写为PostDeleteView而不是DeletePost。同样地,写PostListView,PostDetailView等。添加模型名作为前缀,视图名作为后缀,并且必须在PascalCase中。

405表示不允许使用此方法,请将窗体POST方法更改为DELETE方法。

<form method="DELETE">

相关内容

  • 没有找到相关文章

最新更新