def post_detail_view( request, year, month, day, post): #here we take threee arguments year ,month ,day and posts post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) # i provovide values of the slug,status,year,month,and day based these create a post object if one of these values missing display error page 404 comments = post.comments.filter( active=True) #we get all comments related this post csubmit = False #comment not submited if request.method == 'POST': form = commentform(request.POST) #get end user submited comment if form.is_valid(): new_comment = form.save( commit=False ) #we get end user submitted comment and not saved into the data base new_comment.post = post #post is assigened to post field new_comment.save() #save the post csubmit = True else: #if method is not posts form = commentform() #display form return render(request, 'blog/post_detail.html', { 'post': post, 'form': form, 'csubmit': csubmit, 'comments': comments })
def post_detail_view(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) comments = post.comments.filter(active=True) csubmit = False if request.method == 'POST': form = commentform(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() csubmit = True else: form = commentform() return render(request, 'testapp/post_detail.html', { 'post': post, 'csubmit': csubmit, 'form': form, 'comments': comments })
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = commentform(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('blog:postdetail', pk=post.pk) else: form = commentform() return render(request, 'blog/commentform.html', {'form': form})