具有取决于ID的动态success_url的DeleteView [英] DeleteView with a dynamic success_url dependent on id

查看:41
本文介绍了具有取决于ID的动态success_url的DeleteView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app for posts, with a url for each post:

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

在每个帖子上,我都有评论.我希望能够从帖子页面删除每个评论,然后返回到我所在的帖子.

On each post, I have comments. I would like to be able to delete each comment from the post page and return to the post that I was on.

我有以下用于删除评论的网址:

I have the following url for deleting comments:

    url(r'^comment/(?P<pk>\d+)/delete/$', CommentDelete.as_view(),
    name='comment_delete'),

从以前的研究中我知道我需要覆盖get_success_url,但是我不确定如何引用我刚刚发表的文章ID.我想我需要使用kwargs,但不确定如何使用.我目前有这个,但是不行...

And I know from previous research that I need override the get_success_url, but I'm not sure how to reference the post id that I was just on. I think I need to use kwargs, but not sure how. I have this currently, but it doesn't work...

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 ,其中包含

Django's DeleteView inherits from SingleObjectMixin, which contains the get_object method.

这篇关于具有取决于ID的动态success_url的DeleteView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆