gpt4 book ai didi

django - 具有取决于 id 的动态 success_url 的 DeleteView

转载 作者:行者123 更新时间:2023-12-04 05:38:24 27 4
gpt4 key购买 nike

我有一个帖子应用程序,每个帖子都有一个网址:

url(r'^post/(?P<id>\w+)/$', 'single_post', name='single_post'),

在每个帖子上,我都有评论。我希望能够从帖子页面中删除每条评论并返回到我所在的帖子。

我有以下用于删除评论的网址:
    url(r'^comment/(?P<pk>\d+)/delete/$', CommentDelete.as_view(),
name='comment_delete'),

而且我从之前的研究中知道我需要覆盖 get_success_url,但我不确定如何引用我刚刚发布的帖子 ID。我想我需要使用 kwargs,但不确定如何使用。我目前有这个,但它不起作用......
class CommentDelete(PermissionMixin, DeleteView):
model = Comment
def get_success_url(self):
return reverse_lazy( 'single_post',
kwargs = {'post.id': self.kwargs.get('post.id', None)},)

想法赞赏!

最佳答案

这应该有效:

def get_success_url(self):
# Assuming there is a ForeignKey from Comment to Post in your model
post = self.object.post
return reverse_lazy( 'single_post', kwargs={'post.id': post.id})

Django 的 DeleteView继承自 SingleObjectMixin ,其中包含 get_object method .

关于django - 具有取决于 id 的动态 success_url 的 DeleteView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26290415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com