我有一个用于帖子的应用程序,每个帖子都有一个网址:
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
方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句