def detail_comment(request, page_num): form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] article = Aritcle.objects.get(id=page_num) c = Comment(name=name, comment=comment, belong_to=article) c.save() return redirect(to='detail', page_num=page_num) else: return detail(request, page_num, error_form=form)
def comment(request, id): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] comment = form.cleaned_data["comment"] article = Article.objects.get(id=id) c = Comment(name=name, comment=comment, belong_to=article) c.save() return redirect(to="detail", id=id) return redirect(to="detail", id=id)
def comment(request, id): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.cleaned_data["comment"] c = Comment(comment=comment, belong_to_id=id, commenter_id=request.user.id) c.save() return redirect(to="detail", id=id) else: return detail(request, id, error_form=form) return redirect(to="detail", id=id)
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,'firstapp/comment_form.html',{'form':form})
def comment(request, id): form = CommentForm(request.POST) if form.is_valid(): comment = form.cleaned_data["comment"] article = Article.objects.get(id=id) c = Comment(comment=comment, belong_to_article=article, belong_to_user=request.user) c.save() else: return detail(request, id, error_form=form) return redirect(to="detail", id=id)
def detail_comment(request, page_num): form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] content = form.cleaned_data["content"] a = Article.objects.get(id=page_num) c = Comment(name=name, content=content, belong_to=a) c.save() else: return detail(request, page_num, error_form=form) return redirect(to=detail, id=page_num)
def detail_comment(request, page_num): # 判断是否登录 if request.user.is_authenticated: # 表单绑定 form = CommentForm(request.POST) # 验证表单 if form.is_valid(): content = form.cleaned_data['comment'] publisher = request.user belong_to = Article.objects.get(id=page_num) comment = Comment_New(publisher=publisher, content=content, belong_to=belong_to) comment.save() return redirect('detail', page_num) else: return detail(request, page_num=page_num, error_form=form)
def detail(request): if request.method == "GET": form = CommentForm if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] content = form.cleaned_data["content"] c = Comment(name=name, content=content) c.save() return redirect(to="detail") context = {} comment_list = Comment.objects.all() context['comment_list'] = comment_list context['form'] = form return render(request, 'detail.html', context)
def detail(request): context = {} if request.method == "GET": # 如果是get,则进行表单的创建,这个时候是未绑定表单 form = CommentForm elif request.method == "POST": # 绑定表单,只有当表单绑定的时候才能进行数据检验 form = CommentForm(request.POST) # 校验表单数据,符合规则返回True,否则False if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] c = Comment(name=name, comment=comment) c.save() return redirect(to='detail') comment_list = Comment.objects.all() context['form'] = form context['comment_list'] = comment_list return render(request, 'article_detail.html', context)
def detail(request, id): article = Article.objects.get(id=id) if request.method == "GET": form = CommentForm if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data["name"] comment = form.cleaned_data["comment"] c = Comment(name=name, comment=comment, belong_to=article) c.save() return redirect(to="detail", id=id) context = {} context["article"] = article context['form'] = form return render(request, 'detail.html', context)