def detail_comment(request): form = CommentForm(request.POST) print(form) if form.is_valid(): comment = form.cleaned_data['comment'] c = Comment(comment=comment) c.save() return redirect(to='detail') else: return detail(request)
def detail_comment(request, page_num): form = CommentForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Article.objects.get(id=page_num) c = Comment(name=name, comment=comment, belong_to=a) c.save() else: return detail(request, page_num, error_form=form) return redirect(to='detail', page_num=page_num)
def detail_comment(request, page_num): """post提交评论""" form = CommentForm(request.POST) # 提交数据 print(form) if form.is_valid(): # 判断表单的数据是否通过验证 name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Article.objects.get(id=page_num) # 查找出该文章对应的id c = Comment(name=name, comment=comment, belong_to=a) # 写评论 c.save() # 保存数据库 else: return detail(request, page_num, error_form=form) return redirect(to='detail', page_num=page_num) # 重定向本页
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'] comment = form.cleaned_data['comment'] c = Comment(name=name, comment=comment) 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_comment(request, page_num): """post方法form表单提交""" form = CommentForm(request.POST) #提交数据 print(form) if form.is_valid(): #判断表单的数据是否通过验证; #print(form.is_valid) #返回布尔值,True 或者False #print(form.cleaned_data) name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Aritcle.objects.get(id=page_num) #查找出该文章的id号 c = Comment(name=name, comment=comment, belong_to=a) #把数据储存到Comment模型的实例c中 c.save() #保存到数据库 else: return detail(request, page_num, error_form=form) return redirect(to='detail', page_num=page_num)
def detail_comment(request, head_line): print('type is post') form = CommentForm(request.POST) print(form) # 把通过验证的信息储存为Comment 实例 if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] article = Article.objects.get(headline_2=head_line) # article = Article.objects.get(headline = head_line) c = Comment(name=name, comment=comment, belong_to=article) c.save() print('c is :', c) else: return detail(request, head_line, error_form=form) return redirect(to='detail', head_line=head_line)
def detail(request): if request.method == 'GET': form = CommentForm if request.method == 'POST': form = CommentForm(request.POST) # print(form.errors) # print(form) if form.is_valid(): name = form.cleaned_data['name'] comment = form.cleaned_data['comment'] a = Aritcle.objects.get(id=page_num) c = Comment(name=name, comment=comment) c.save() return redirect(to='detail') context = {} comment_list = Comment.objects.all() context['comment_list'] = comment_list context['form'] = form return render(request, 'article_detail.html', context)
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["comment"] c = Comment(name=name, content=content) c.save() return redirect(to="detail") context = {} comment_list = Comment.objects.all() article = Article.objects.get(id=page_num) context['article'] = article context['comment_list'] = comment_list context['form'] = form return render(request, 'detail.html', context)
def detail(request, page_num, error_form=None): contect = {} form = CommentForm() article = Article.objects.get(id=page_num) best_comment = Comment.objects.filter(best_comment=True, belong_to=article) if best_comment: contect['best_comment'] = best_comment[0] contect['article'] = article if error_form is not None: contect['form'] = error_form else: contect['form'] = form return render(request, 'detail.html', contect)
def detail_old(request, page_num): 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'] a = Aritcle.objects.get(id=page_num) c = Comment(name=name, comment=comment, belong_to=a) c.save() return redirect(to='detail', page_num=page_num) context = {} a = Aritcle.objects.get(id=page_num) best_comment = Comment.objects.filter(best_comment=True, belong_to=a) if best_comment: context['best_comment'] = best_comment[0] article = Aritcle.objects.get(id=page_num) context['article'] = article # context['comment_list'] = comment_list context['form'] = form return render(request, 'article_detail.html', context)