def blog_detail(request, pk): post = Post.objects.get(pk=pk) posts = Post.objects.all().order_by('-created_on') #comments form = CommentsForm() if request.method == 'POST': form = CommentsForm(request.POST) if form.is_valid(): comment = Comments(author=form.cleaned_data["author"], body=form.cleaned_data["body"], post=post) comment.save() return redirect('blog_index') comments = Comments.objects.filter(post=post) context = { "post": post, "list": posts[:3], "comments": comments, "form": form } return render(request, "blog_detail.html", context)
def comments_add(request, id): '''评论''' context_dict = {} blog = get_object_or_404(Blog, pk=id) if request.method == 'POST' and request.is_ajax(): comment_form = CommentsForm(request.POST) anybody_form = AnybodyForm(request.POST) if comment_form.is_valid() and anybody_form.is_valid(): comt = comment_form.save(commit=False) anyuser = anybody_form.cleaned_data try: # 邮箱存在则不作记录, 仅更新访客名与站点 email = Anybody.objects.get(email=anyuser['email']) if anyuser['website']: Anybody.objects.filter(email=anyuser['email']).update( anyname=anyuser['anyname'], website=anyuser['website'] ) except: anybody_form.save() email = Anybody.objects.get(email=anyuser['email']) comt.email = email comt.save() comt.acticle_id.add(blog) comt.save() # 访客信息存入会话, 用于楼中楼 request.session['reply_user'] = anyuser['anyname'] request.session['reply_email'] = anyuser['email'] else: # print 'error!', comment_form.errors, anybody_form.errors pass else: comment_form = CommentsForm() anybody_form = AnybodyForm() comments = blog.comments_set.all() for cmt in comments: anybody = Anybody.objects.filter(email=cmt.email)[0] cmt.name = anybody.anyname cmt.website = anybody.website cmt.reply = cmt.replyid.all() comments_count = comments.count() context_dict.update({ 'comments_all':comments, 'comment_form':comment_form, 'anybody_form':anybody_form, 'comments_count':comments_count, }) return context_dict
def newcomment(request, post_id): if request.POST and ('justamoment' not in request.session): form = CommentsForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.comments_blogpost = BlogPost.objects.get(id = post_id) form.save() request.session.set_expiry(120) request.session['justamoment'] = True return redirect('/post/%s/' % post_id)
def send_comment(request, post_pk): post = BlogPost.objects.get(pk=post_pk) comment_form = CommentsForm(request.POST) if comment_form.is_valid(): d = comment_form.cleaned_data answer = d['question'].lower() if answer == 'berlin': comment = comment_form.save(commit=False) comment.post = post comment.author = 'blablabla' comment.save() return redirect('get_content_by_title', post_pk=post_pk) else: return render_to_response('post/post.html', {'post': post, 'comments_form': comment_form}, context_instance=RequestContext(request))
def get_content_by_title(request, post_pk): post = BlogPost.objects.get(pk=post_pk) comments_form = CommentsForm() return render_to_response('post/post.html', { 'post': post, 'comments_form': comments_form }, context_instance=RequestContext(request))
def post(request, id): if request.method == 'GET': # 获取文章内容 post = Post.objects.filter(id=id).first() comment_form = CommentsForm() # 添加阅读数 post.page_view += 1 post.save() context = {'post': post, 'comment_form': comment_form} return render(request, 'blog/post.html', context) elif request.method == 'POST': comment_form = CommentsForm(request.POST) print(comment_form) if comment_form.is_valid(): comm = request.POST['comment'] id = int(request.POST['id']) post = Post.objects.get(id=id) # user = BlogUser.objects.get(id=1) obj = Comments(post=post, content=comm) try: obj.save() except: return HttpResponse('error') return HttpResponseRedirect('/blog/post/' + str(id)) else: id = int(request.POST['id']) post = Post.objects.filter(id=id).first() comment_form = CommentsForm() context = {'post': post, 'comment_form': comment_form} return render(request, 'blog/post.html', context) else: pass
def send_comment(request, post_pk): post = BlogPost.objects.get(pk=post_pk) comment_form = CommentsForm(request.POST) if comment_form.is_valid(): d = comment_form.cleaned_data answer = d['question'].lower() if answer == 'berlin': comment = comment_form.save(commit=False) comment.post = post comment.author = 'blablabla' comment.save() return redirect('get_content_by_title', post_pk=post_pk) else: return render_to_response('post/post.html', { 'post': post, 'comments_form': comment_form }, context_instance=RequestContext(request))
def post_detail_view(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published', publish__year=year, publish__month=month, publish__day=day) comments = post.comments.filter(active=True) cssubmit = False if request.method == 'POST': form = CommentsForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() cssubmit = True else: form = CommentsForm() # return render(request,'blog/post_detail.html', {'post': post, 'form':form, 'cssubmit':cssubmit, 'comments': comments}), return render(request, 'blog/post_detail.html', { 'post': post, 'form': form, 'comments': comments, 'cssubmit': cssubmit })
def create_comments(request): form = CommentsForm(request.POST or None) context = { 'form': form, } if request.POST and request.user.is_authenticated: if form.is_valid(): author = request.user text = form.cleaned_data.get("text") comment = Comment.objects.create_object(author=author, text=text) comment.save() qs = Comment.objects.all() context = {'form': form, 'qs': qs} return redirect('detail-cbv') else: form = CommentsForm(request.POST or None) return render(request, 'blog/detail-cbv', context) ## DELETE COMMENTS URL BAKI 6E ## DRAFTS URL BAKI 6E
def add_comment_to_post(request,pk): post = get_object_or_404(Post, pk = pk) if request.method == 'POST': form = CommentsForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail',pk = post.pk) else: form = CommentsForm() return render(request , 'blog/comment_form.html',{'form' : form})
def post_detail_view(request, year, month, day, post): post = get_object_or_404(Post, slug=post, status='published') comments = post.comments.filter( active=True ) # display all comments related to this post where active=True only csubmit = False #csubmit means commentsubmit if request.method == 'POST': form = CommentsForm(request.POST) # it contains CommentsForm form data if form.is_valid(): new_comment = form.save( commit=False ) #commit=False means don't save the comments in the database now new_comment.post = post # related to the current post assign the comment for post field i.e new_comment.post new_comment.save() csubmit = True #csubmit means commentsubmit else: form = CommentsForm() return render(request, 'blog/post_detail.html', { 'post': post, 'form': form, 'csubmit': csubmit, 'comments': comments }) #TO DISPLAY POST WITHOUT NO. OF VISITORS TO WEBSITE