def post(self, request, pk): f = get_object_or_404(Cat, id=pk) comment_form = CommentForm(request.POST) comment = Comment(text=request.POST['comment'], owner=request.user, cat=f) comment.save() return redirect(reverse_lazy('cat_detail', args=[pk]))
def get(self, request, pk): cat = Cat.objects.get(id=pk) comments = Comment.objects.filter(cat=cat).order_by('-updated_at') comment_form = CommentForm() context = { 'cat': cat, 'comments': comments, 'comment_form': comment_form } return render(request, self.template_name, context)