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 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) post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.objects.filter(tags__in=post_tags_ids).exclude( id=post.id) similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by( 'same_tags', 'publish')[:4] comments = post.comments.filter(active=True) csubmit = False form = CommentForm(data=request.POST or None) if request.method == 'POST': # form=CommentForm(data=request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() csubmit = True form = CommentForm() else: form = CommentForm() return render( request, 'testapp/detail.html', { 'post': post, 'form': form, 'comments': comments, 'csubmit': csubmit, 'similar_posts': similar_posts })
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,) #Coments form & comments realted to post 'related_name=comments' 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) #get comment new_comment.post=post #assing post value associte with comment new_comment.save() #save comments csubmit=True #if it is not post required to display form else: form=CommentForm() return render(request,'testapp/post_detail.html',{'post':post,'form':form,'csubmit':csubmit,'comments':comments})