def post(post_id): """Post detail page.""" form = CommentForm() if form.validate_on_submit(): # If valid form is submitted, add comment and redirect back to page new_comment = Comment() new_comment.name = form.name.data new_comment.text = form.text.data new_comment.post_id = post_id new_comment.date = datetime.datetime.now() db.session.add(new_comment) db.session.commit() return redirect(request.url) else: # No form submitted or form is invalid, show post (with errors if appropriate) post = Post.query.get_or_404(post_id) tags = post.tags comments = post.comments.order_by(Comment.date.desc()).all() recent, top_tags = sidebar_data() return render_template( 'post.html', post=post, tags=tags, comments=comments, recent=recent, top_tags=top_tags, 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 add_comment(request, pk): post = get_object_or_404(Post, pk=pk) form = CommentForm(request.POST or None) if request.method == "POST": if form.is_valid(): comment = form.save(commit=False) comment.post = post comment.save() return redirect('post_detail', pk=post.pk) else: return render(request, 'blog/add_comment.html', {'form': form})
def blog_detail(request, post_slug): id = '' user_email='' if request.user.is_authenticated: id = request.user.id user_email=request.user.email data1=[] blog = Blog.objects.get(slug=post_slug) print(blog) date=str(blog.date) day = date[8:10] year = date[:4] month=date[5:7] email="" if request.user.is_authenticated: email = request.user.email name=request.user.username data1.append({'id':blog,'name':blog.name,'blog_tittle':blog.title,'blog_body':blog.body,'blog_date':blog.date,'blog_image':blog.image,'blog_email':blog.email}) is_liked = False if blog.like.filter(id=request.user.id).exists(): is_liked=True blog_comments = Comment.objects.filter(blog__id=blog.id) if request.method == "POST": comment_show = CommentForm(request.POST) if request.user.is_authenticated: if comment_show.is_valid(): Comment.objects.create(blog=blog, comment=comment_show.cleaned_data['comment'], email=email,name=name) return redirect("/blogapp/details-/" + post_slug) else: for field in comment_show.errors : comment_shows=field return render(request, "single-blog.html", {'blog': data1, 'blog_comments': blog_comments, "comment_forms": comment_show, 'errors': comment_shows, 'is_liked':is_liked,'day':day,'month':month,'year':year ,'email':email,'blog1':blog,'id':id,'user_email':user_email}) else: return redirect("myshop:login") comment_forms = CommentForm() return render(request, 'single-blog.html', {'blog': data1 , 'blog_comments': blog_comments, "comment_forms": comment_forms, 'is_liked':is_liked,'day':day,'month':month,'year':year, 'blog1':blog,'email':email,'id':id,'user_email':user_email})
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 blog_detail(request, post_slug): blog = Blog.objects.get(slug=post_slug) is_liked = False if blog.like.filter(id=request.user.id).exists(): is_liked = True name = "" if request.user.is_authenticated: name = request.user.first_name blog_comments = Comment.objects.filter(blog__id=blog.id) if request.method == "POST": comment_show = CommentForm(request.POST) if request.user.is_authenticated: if comment_show.is_valid(): Comment.objects.create( blog=blog, comment=comment_show.cleaned_data['comment'], email=comment_show.cleaned_data['email']) return redirect("/details-/" + post_slug) else: for field in comment_show.errors: comment_shows = field name = request.user.first_name return render( request, "second.html", { 'blg1': blog, 'blog_comments': blog_comments, "comment_forms": comment_show, 'errors': comment_shows, 'name': name, 'is_liked': is_liked }) else: return redirect("blogapp:login") print("tongo") comment_forms = CommentForm() return render( request, 'second.html', { 'blg1': blog, 'blog_comments': blog_comments, "comment_forms": comment_forms, 'name': name, 'is_liked': is_liked })
def post_detail(request, slug): post = Post.objects.get(slug=slug) form = CommentForm(request.POST or None) comments = Comment.objects.filter(post=post) return render(request, 'post_detail.html', { 'post': post, 'form': form, 'comments': comments })
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 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 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 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('detail-post-page', pk=post.pk) else: form = CommentForm() return render(request, 'blogapp/comment_form.html', {'form': form})
def add_comment_to_post(request, pk): post = get_object_or_404(Post, pk=pk) if request.method == 'POST': form = CommentForm( request.POST) #pass in what they filled into the form 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 post_by_name(request, post_name): try: post = Post.objects.get(name=post_name) except ObjectDoesNotExist: return not_found(request, message=_("Sorry, the requested post does not exist.")) #check if comments are enabled if not post.disable_comments: if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): #result will either be a http redirect or an error string result = process_comment(request, post, form) if isinstance(result, unicode): form = CommentForm(request.POST, auto_id=False) form.errors['generic'] = result else: #redirect return result else: form.errors['generic'] = _("Check that the required fields are filled in correctly.") else: #values are pickled to enable unicode strings to be stored #unpickle_cookies(request) #form = CommentForm(request.COOKIES, auto_id=False) if request.user.is_authenticated(): form = CommentForm(initial = { "author_name": request.user.username, "author_email": request.user.email, "author_website": DatosUsuario.objects.get(user=request.user).web }) else: form = CommentForm() else: form = None #takes comments where comment_type is 'comment' or 'linkback' comments = post.comment_set.filter(comment_type='comment') | post.comment_set.filter(comment_type='linkback') context = { 'posts': [post], 'comments': comments, 'title': post.title, 'comment_form': form, 'options': options(), } return render_to_response(BLOG_TPL, context, context_instance=RequestContext(request))
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 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')
def create_comment(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() name = "Create comment" return render(request, 'blogapp/comment_form.html', { 'form': form, 'name': name })
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 index(request): context = {'posts': Post.objects.all(), 'commentForm': CommentForm()} template_name = 'blogapp/post_index.html' return render(request, template_name, context)