def create(request): if request.method == 'GET': form = CommentForm() elif request.method == 'POST': form = CommentForm(request.POST, request.FILES) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.created = timezone.now() comment.save() return render(request, 'create_success.html') return render(request, 'create.html', context={ 'form': form})
def edit(request, id=None): comment = get_object_or_404(Comment, pk=id) if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.published_date = timezone.now() comment.save() return redirect('/comments') else: form = CommentForm(instance=comment) return render(request, 'edit.html', {'form': form})
def post(self, request, id=None, *args, **kwargs): if id is not None: post_instance = get_object_or_404(Post, pk=id) initial_data = { 'content_type': post_instance.get_content_type(), 'object_id': post_instance.id } comment_form = CommentForm(request.POST) if request.user.is_authenticated: if comment_form.is_valid(): my_comment = comment_form.save(commit=False) my_comment.user = request.user my_comment.save() messages.success(request, 'Comment Created Successfully.') return redirect('postDetail', id=post_instance.pk) return render(request, self.template_name, self.context)
def post_comment(request, pk): article = 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 = article comment.save() return redirect(article) else: comment_list = article.comment_set.all() context = { 'comment_list': comment_list, 'article': article, 'form': form } return render(request, 'blog/index.html', context=context) else: redirect(article)
def save_comment(request): err = True comment_status = 0 try: if request.POST['private_comment']: comment_status = -2 except: pass try: form = CommentForm(request.POST) comment = form.save(commit=False) comment.ip = request.META.get('REMOTE_ADDR', '') comment.created = datetime.today() comment.status = comment_status comment.save() err = False if comment_status == -2 or settings.COMMENT_NOTIFICATION_EMAIL: send_mail( _('Comment')+': '+comment.name, comment.comment, comment.email, [settings.COMMENT_NOTIFICATION_EMAIL], fail_silently=True) finally: if request.POST.get('ret'): ret = request.POST.get('ret') elif request.META.get('HTTP_REFERER'): ret = request.META.get('HTTP_REFERER') else: ret = '/' if err: ret += '?err=1' return HttpResponseRedirect(ret)