def add_comment(request,article_id): a = Article.objects.get(id=article_id) if request.method == "POST": f =CommentForm(request.POST) if f.is_valid(): # This means that save this instance of form but do not # push anything to databases(commit = False) as we are # not finished yet c = f.save(commit =False) c.pub_date = timezone.now() c.article = a c.save() # by default commit = True, therefore the comment will be # saved to the database. return HttpResponseRedirect('/articles/get/%s' % article_id ) else: f = CommentForm() args = {} args.update(csrf(request)) args['article'] = a args['form'] = f return render_to_response('add_comment.html',args)
def post(request, post_slug): post = Post.objects.get(slug=post_slug) user = request.user form = CommentForm(request.POST or None) if request.method == "POST": if form.is_valid(): temp = form.save(commit=False) temp.post = post temp.user = user parent = form['parent'].value() print parent if parent == '': # Set a blank path then save it to get an ID temp.path = [] temp.save() temp.path = [temp.id] else: # Get the parent node node = Comment.objects.get(id=parent) temp.depth = node.depth + 1 temp.path = node.path # Store parents path then apply comment ID temp.save() temp.path.append(temp.id) # Final save for parents and children temp.save() form = CommentForm() # Reset the form, clean the fields. (AJAX doesn't refresh the page) # Retrieve all comments and sort them by path comment_tree = Comment.objects.filter(post=post).order_by('path') return render(request, 'reddit/post.html', locals())
def comment_view(request): user = check_validation(request) if user and request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): post_id = form.cleaned_data.get('post').id comment_text = form.cleaned_data.get('comment_text') comment = CommentModel.objects.create(user=user, post_id=post_id, comment_text=comment_text) comment.save() return redirect('/feed/')
def detail(request,article_id): try: article = models.Article.objects.get(pk=article_id)#pk=id comment_list = models.Comment.objects.all() except models.Article.DoesNotExist: raise Http404 if request.method=='POST': form = CommentForm(request.POST) if form.is_valid(): form.save() return HttpResponseRedirect('/blog/%s/'%article_id) else: form = CommentForm() return render_to_response('detail.html',{'article':article,'comment_list':comment_list,'form':form})
def comment(request): form = CommentForm(request.POST) if form.is_valid(): comment = Comment() comment.content = form.cleaned_data.get('content') comment.user = request.user id=form.cleaned_data.get('article_id') try: article = Article.objects.get(id=id) comment.article = article comment.save() article.save() rtn = {'status':True,'redirect':reverse('blog:show', args=[id])} except Article.DoesNotExist: rtn = {'status':False,'error':'Article is not exist'} else: rtn = {'status':False,'error':form.errors} return JsonResponse(rtn)