def add_comment_to_post(request,pk): post = get_object_or_404(Post,pk = pk) # یک آبجکت از پست بساز if request.method == 'POST': from = commentForm(request.POST) if form.is_valid(): comment = form.save(commit = False) comment.post = post comment.save() return redirect('post_detail',pk = post.pk) else: form = commentForm() return render ( request , ' blog/comment_form.html',{'form': form})
def add_comment_to_post(request, pk): posts = get_object_or_404(post, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save( commit=False ) #we are saving in memory but not in database so ewe can make some changes before saving it comment.post = posts #if we look our models there our comment's post is a foreignkwy for post for making sure it became here true too comment.save() return redirect('post_detail', pk=posts.pk) else: form = CommentForm() return render(request, 'blog/comments_form.html', {'form': form})
def form_valid(self, form): article_pk = self.kwargs['article_pk'] comment = form.save(commit=False) comment.article_id = get_object_or_404(article.Article, pk=article_pk) comment.save() return redirect('blog:detail', pk=article_pk)