def detail(request, blog_id): post = get_object_or_404(Post, pk=blog_id) if request.method == 'POST' and request.user.is_authenticated: comment_form = CommentForm(request.POST, request.FILES) if comment_form.is_valid(): comment_instance = comment_form.save(commit=False) comment_instance.comment_user = request.user comment_instance.content_type = ContentType.objects.get_for_model(Post) comment_instance.content_object = post comment_instance.object_id = post.id parent_id = request.POST.get('parent_id') if parent_id is not None: comment_instance.parent_comment = Comment.objects.get(pk=int(parent_id)) comment_form.save() comment_form.save_m2m() return HttpResponseRedirect(post.get_absolute_url()) post_comments = post.blog_comment.filter(parent_comment=None).order_by('-comment_date_time') page_post_comments = page_list(request, post_comments, 10) context = { 'booklog': post.content_object, 'post': post, 'post_comments': page_post_comments, 'comment_form': CommentForm(), } return render(request, 'blog/detail.html', context)
def post(self, request, booklog_id): booklog = get_object_or_404(BookLog, pk=booklog_id) comment_form = CommentForm(request.POST, request.FILES) if comment_form.is_valid(): comment_instance = comment_form.save(commit=False) comment_instance.comment_user = request.user comment_instance.content_type = ContentType.objects.get_for_model(BookLog) comment_instance.content_object = booklog comment_instance.object_id = booklog.id parent_id = request.POST.get('parent_id') if parent_id is not None: comment_instance.parent_comment = Comment.objects.get(pk=int(parent_id)) comment_form.save() comment_form.save_m2m() return HttpResponseRedirect(booklog.get_absolute_url()) return self.get(request, booklog_id)
def post(self, request, book_id): book = get_object_or_404(Book, pk=book_id) if request.user.is_authenticated: comment_form = CommentForm(request.POST, request.FILES) if comment_form.is_valid(): comment_instance = comment_form.save(commit=False) parent_id = request.POST.get('parent_id') if parent_id is not None: parent_obj = Comment.objects.get(pk=int(parent_id)) else: return self.get(request, book_id) comment_instance.parent_comment = parent_obj comment_instance.comment_user = request.user comment_instance.content_type = ContentType.objects.get_for_model( BookLog) comment_instance.content_object = BookLog.objects.get( booklog_book=book, booklog_owner=parent_obj.comment_user) comment_instance.object_id = book.id comment_form.save() comment_form.save_m2m() return HttpResponseRedirect(book.get_absolute_url()) return self.get(request, book_id)