def submitComment(request,pk): b = json.dumps({'error':True}) if (request.method == 'POST' and request.user.is_authenticated()): article = get_object_or_404(Article,pk = pk) f = Comment(article = article, author=request.user) form = CreateCommentForm(request.POST,instance = f) if form.is_valid(): c = form.save() b = json.dumps({'comment':c.text, 'author':c.author.username, 'error':False}) return HttpResponse(b,mimetype='application/json')
def articleView(request,pk): article = get_object_or_404(Article, pk=pk) comments = article.comment_set.all() for comment in comments: if comment.author == request.user: comment.canEdit = True comment.editForm = EditCommentForm(instance = comment) if (request.method == 'POST' and request.user.is_authenticated()): c = Comment(article = article, author=request.user) form = CreateCommentForm(request.POST,instance = c) if form.is_valid(): form.save() return HttpResponseRedirect('') else: form = CreateCommentForm() editForm = EditArticleForm(instance = article) canEdit = (request.user == article.author) return {'article':article, 'comments':comments, 'form':form, 'editForm':editForm, 'canEdit':canEdit}