【问题标题】:success url does not work in my class based DeleteView成功 url 在我基于类的 DeleteView 中不起作用
【发布时间】:2021-01-14 06:11:28
【问题描述】:

我的基于类的删除视图的success_url由于某种原因不起作用。

在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)

在 urls.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 %}

每当我单击确定删除特定项目时,它都不会返回到以下帖子列表:

网页上的错误图片

【问题讨论】:

    标签: python django django-views django-templates


    【解决方案1】:

    你确实没有这个网址:

    forum-posts/delete-post/
    

    实际上,您忘记指定一个整数来指示必须删除的 post_id。例如:

    forum-posts/delete-post/1/
    

    【讨论】:

    • 谢谢,就是这样。忘记在我的 urls.py 文件中为我的路径添加“/”
    • 所以你想接受答案并投票吗? @迈克琼斯
    猜你喜欢
    • 2019-12-14
    • 2011-07-28
    • 2023-03-19
    • 2020-10-11
    • 2017-08-27
    • 2018-12-16
    • 2021-08-26
    • 2021-01-10
    • 1970-01-01
    相关资源
    最近更新 更多