def article_detail(request, page_num): if request.method == 'GET': form = CommentForms print 'request get' if request.method == 'POST': print 'request POST' form = CommentForms(request.POST) print form # is_valid表单对象的首要任务就是验证数据。对于绑定的表单实例,可以调用is_valid()方法来执行验证并返回一个表示数据是否合法的布尔值。 if form.is_valid(): # cleaned_data表单类中的每个字段不仅负责验证数据,还负责“清洁”它们 —— 将它们转换为正确的格式。这是个非常好用的功能,因为它允许字段以多种方式输入数据,并总能得到一致的输出。 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() return redirect(to='article_detail', page_num=page_num) context = {} a = Article.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] # comment_list = Comment.objects.all() article = Article.objects.get(id=page_num) context['article'] = article # context['comment_list'] = comment_list context['form'] = form print dir(form) return render(request, 'article_detail.html', context)
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 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 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 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): 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): """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_comment(request, page_num): # request.POST是一个字典<QueryDict: {u'comment': [u'aaa'], u'csrfmiddlewaretoken': [u'7FnDoRXR4XO6lNUIVgRbMpPPtHfw6QHKAD0naPlMDSpgpyOgAXXl9SUar9ignemJ'], u'name': [u'abc']}> form = CommentForms(request.POST) # is_valid表单对象的首要任务就是验证数据。对于绑定的表单实例,可以调用is_valid()方法来执行验证并返回一个表示数据是否合法的布尔值。 if form.is_valid(): # cleaned_data表单类中的每个字段不仅负责验证数据,还负责“清洁”它们 —— 将它们转换为正确的格式。这是个非常好用的功能,因为它允许字段以多种方式输入数据,并总能得到一致的输出。 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() return redirect(to='detail', page_num=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'] 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): 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)
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 discuss(request, belong): if request.method == "GET": form = my_form.CommentForm() if request.method == "POST": form = my_form.CommentForm(request.POST) print("belong--->", belong) if form.is_valid(): user_name = request.session.get("user") if user_name: content = form.cleaned_data["comment"] comment = Comment(user_name=user_name, belong=belong, comment=content) comment.save() return redirect(to="blog", name=belong.name) else: print(isinstance(form.errors, list)) form.errors["未登录"] = ",请先登录在评论" print("errors--->", form.errors) print("form--->", form) return form
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)