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) post_tags_ids = post.tags.values_list('id', flat=True) similar_posts = Post.objects.filter(tags__in=post_tags_ids).exclude( id=post.id) similar_posts = similar_posts.annotate(same_tags=Count('tags')).order_by( '-same_tags', 'publish')[:4] comments = post.comments.filter(active=True) csubmit = False if request.method == 'POST': form = CommentForm(data=request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() csubmit = True else: form = CommentForm() return render( request, 'blogapp/post_detail.html', { 'post': post, 'form': form, 'comments': comments, 'csubmit': csubmit, 'similar_posts': similar_posts })
def post_details_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) csubmit = False if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.post = post new_comment.save() csubmit = True else: form = CommentForm() return render(request, "blogapp/post_details.html", { 'post': post, 'form': form, 'csubmit': csubmit, 'comments': comments })
def blog_details(request, slug): blog = Blog.objects.get(slug=slug) comment_form = CommentForm() already_liked = Like.objects.filter(blog=blog, user=request.user) if already_liked: liked = True else: liked = False if request.method == 'POST': comment_form = CommentForm(request.POST) if comment_form.is_valid(): comment = comment_form.save(commit=False) comment.user = request.user comment.blog = blog comment.save() return HttpResponseRedirect( reverse('blogapp:blog_details', kwargs={'slug': slug})) return render(request, 'blogapp/blog_details.html', context={ 'blog': blog, 'comment_form': comment_form, 'liked': liked })
def detail(request, articleid): # return HttpResponse("详情") if request.method == "GET": try: article = Article.objects.get(id=articleid) article.views += 1 article.save() cf = CommentForm() return render(request, 'single.html', locals()) except: return HttpResponse("查无此书") else: cf = CommentForm(request.POST) if cf.is_valid(): # 此时cf是一个表单,不是实例 # cf.article = Article.objects.get(id=articleid) # 保存前对commit默认值修改为False comment就是实例了 comment = cf.save(commit=False) comment.article = Article.objects.get(id=articleid) comment.save() url = reverse("blogapp:detail", args=(articleid, )) return redirect(to=url) else: article = Article.objects.get(id=articleid) cf = CommentForm() errors = "输入格式有误!" return render(request, 'single.html', locals())
def add_comment_to_post(request, pk): post = 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 = post comment.save() return redirect('post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blogapp/comment_form.html', {'form': form})
def comment_store(request, post_id): post = Post.objects.get(id=post_id) if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('blogapp:post_index') return redirect('blogapp:post_index')
def post_detail_view(request,year,month,day,post): post=get_object_or_404(Post,slug=post,publish__year=year,publish__month=month,publish__day=day) comments=post.comments.filter(active=True) csubmit=False form=CommentForm() if request.method=='POST': form=CommentForm(request.POST) if form.is_valid(): newcomment=form.save(commit=False) newcomment.post=post newcomment.save() csubmit=True d={'post':post,'form':form,'csubmit':csubmit,'comments':comments} return render(request,'blogapp/post_detail.html',d)
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm(request.POST) # form.instance.author = request.user # you can do this to auto save the author of comment # or can do something like # comment.author = request.user in form.is_valid() method. if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.post = post comment.save() return redirect('blogapp:post_detail', pk=post.pk) else: form = CommentForm() return render(request, 'blogapp/comment_form.html', {'form': form})
def detail(request, articleid): if request.method == 'GET': try: article = Article.objects.get(id=articleid) cf = CommentForm() return render(request, 'single.html', locals()) except Exception as e: print(e) return HttpResponse('文章不合法') elif request.method == 'POST': cf = CommentForm(request.POST) if cf.is_valid(): comment = cf.save(commit=False) comment.article = Article.objects.get(id=articleid) comment.save() url = reverse('blogapp:detail', args=(articleid, )) return redirect(to=url) else: article = Article.objects.get(id=articleid) cf = CommentForm() errors = '输入信息有错误' return render(request, 'single.html')