基于类的删除视图的成功url由于某些原因不起作用
in views.py
# allows a user to delete a project
class DeletePost(DeleteView):
template_name = 'user_posts/post_delete.html'
model = Post
# return to the all posts list
success_url = reverse_lazy('posts_list')
# make sure the user is looking at its own post
def dispatch(self, request, *args, **kwargs):
obj = self.get_object()
if not obj.user == self.request.user:
raise Http404("You are not allowed to Delete this Post")
return super(DeletePost, self).dispatch(request, *args, **kwargs)
在URL.py中:
path('list/', PostsListView.as_view(), name="posts_list"),
path('create-post/', CreatePostView.as_view(), name="post_create"),
path('update-post/<int:pk>', UpdatePost.as_view(), name="post_update" ),
path('delete-post/<int:pk>', DeletePost.as_view(), name="post_delete")
在HTML文件中:
{% extends 'base.html' %}
{% block content %}
<form action="." method="POST" style="width:80%;">
{% csrf_token %}
<h3>Do You want to delete this post: "{{ object.title }}"</h3>
<input class="btn btn-primary" type="submit" value="Confirm"/>
<a href="{% url 'posts_list' %}">Cancel</a>
</form>
{% endblock content %}
每当我单击“确定”删除特定项目时,它不会返回到以下内容的帖子列表:
网页上的错误图像
您确实没有此URL:
实际上,您忘记指定一个整数来指示必须删除的post_id。例如:
相关问题 更多 >
编程相关推荐