def update_comment(request): data = {} comment_form = CommentForm(request.POST, user=request.user) if comment_form.is_valid(): # 检查通过,保存数据 comment = Comment() comment.user = comment_form.cleaned_data['user'] comment.text = comment_form.cleaned_data['text'] comment.content_object = comment_form.cleaned_data['content_object'] parent = comment_form.cleaned_data['parent'] if parent: comment.root = parent.root if parent.root else parent comment.parent = parent comment.reply_to = parent.user comment.save() #评论/回复成功,返回数据 data['status'] = 'SUCCESS' data['username'] = comment.user.get_nickname_or_username() data['comment_time'] = comment.comment_time.strftime( '%Y-%m-%d %H:%M:%S') data['text'] = comment.text data['reply_to'] = comment.reply_to.get_nickname_or_username( ) if parent else None data['id'] = comment.id data['root_id'] = comment.root.id if parent else None data['content_type'] = ContentType.objects.get_for_model(comment).model # 评论/回复失败 else: data['status'] = 'ERROR' data['message'] = list(comment_form.errors.values())[0][0] return JsonResponse(data)
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 details(request, id): #instance = post.objects.get(id=1) instance = get_object_or_404(post, id=id) if instance.draft: if not request.user.is_authenticated(): raise Http404 initial_data = { "content_type": instance.get_content_type, "object_id": instance.id, } comment_form = CommentForm(request.POST or None, initial=initial_data) if comment_form.is_valid() and request.user.is_authenticated(): c_type = comment_form.cleaned_data.get("content_type") content_type = ContentType.objects.get(model=c_type) obj_id = comment_form.cleaned_data.get("object_id") content_data = comment_form.cleaned_data.get("content") parent_obj = None try: parent_id = int(request.POST.get("parent_id")) except: parent_id = None if parent_id: parent_qs = Comment.objects.filter(id=parent_id) if parent_qs.exists() and parent_qs.count() == 1: parent_obj = parent_qs.first() new_comment, created = Comment.objects.get_or_create( user=request.user, content_type=content_type, object_id=obj_id, content=content_data, parent=parent_obj, ) return HttpResponseRedirect(instance.get_absolute_url()) form = PostForm(request.POST or None, request.FILES or None, instance=instance) comments = instance.comments #comment.objects.filter_by_instance(instance) context = { "instance": instance, "title": instance.title, "comment": comments, "comment_form": comment_form, "form_e": form, } #print(instance.content) return render(request, 'details.html', context)
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 update_comment(request): referer = request.META.get('HTTP_REFERER', reverse('blog:index')) comment_form = CommentForm(request.POST, user=request.user) if comment_form.is_valid(): # 检查通过,保存数据 comment = Comment() comment.user = comment_form.cleaned_data['user'] comment.text = comment_form.cleaned_data['text'] comment.content_object = comment_form.cleaned_data['content_object'] parent = comment_form.cleaned_data['parent'] if not parent is None: comment.root = parent.root if not parent.root is None else parent comment.parent = parent comment.reply_to = parent.user comment.save() return redirect(referer + '#comment')
def post_comment(request, article_id): article = get_object_or_404(Article, id=article_id) # 处理 POST 请求 if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): content = request.POST["body"] print(content) ai = article.id ui = request.user.id Comment.objects.create(body=content, article_id=ai, CommentUser_id=ui) return redirect(article) else: return HttpResponse("表单内容有误,请重新填写。") # 处理错误请求 else: return HttpResponse("发表评论仅接受POST请求。")
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 update_comment(request): referer = request.META.get('HTTP_REFERER', reverse('blog:index')) comment_form = CommentForm(request.POST, user=request.user) if comment_form.is_valid(): # Check passed, save data comment = Comment() comment.user = comment_form.cleaned_data['user'] comment.text = comment_form.cleaned_data['text'] comment.content_object = comment_form.cleaned_data['content_object'] parent = comment_form.cleaned_data['parent'] if not parent is None: comment.root = parent.root if not parent.root is None else parent comment.parent = parent comment.reply_to = parent.user comment.save() # # Send notification # # Determine whether the comment is a blog or # if comment.reply_to is None: # # 接受者是文章 # recipient = comment.content_object.get_user() # if comment.content_type.model == 'post': # blog = comment.content_object # verb = '{0} 评论了你的《{1}》'.format(comment.user,blog.title) # else: # raise Exception('不明评论') # # else: # # 接受者是作者 # recipient = comment.reply_to # verb = '{0} 评论了你的评论{1}'.format(comment.user, strip_tags(comment.parent.text)) # # notify.send(comment.user, recipient=recipient, verb=verb, action_object=comment) return redirect(referer + '#comment')