I can't redirect to the post after editing a comment of that post
I can't redirect to the post after editing a comment of that post.
class CommentEditView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Comment
fields = ['comment']
template_name = 'social/comment_edit.html'
def get_success_url(self):
pk = self.kwargs['pk']
return reverse_lazy('post-detail',kwargs={'pk': pk,})
def test_func(self):
post = self.get_object()
return self.request.user == post.author
Here Comment editing is working. But after submitting the edited comment I want it should redirect to the post related to the commentt.
The pk
is the primary key of the comment, not the post. You redirect with:
class CommentEditView(LoginRequiredMixin, UpdateView):
model = Comment
fields = ['comment']
template_name = 'social/comment_edit.html'
def get_queryset(self, *args, **kwargs):
return super().get_queryset(self, *args, **kwargs).filter(
author=self.request.user
)
def get_success_url(self):
return reverse_lazy('post-detail',kwargs={'pk': self.object.post_id})