表单未在django中验证

时间:2013-11-28 11:31:09

标签: python django

我的表单没有返回数据.. 这是代码

views.py

 if request.method=="POST":
    add_gateway_details_form=Add_Gateway_Details(request.POST)
    add_gateway_parameters_form=Add_Gateway_Parameters(request.POST)
            if add_gateway_details_form.is_valid():
                   gateway_name=add_gateway_details_form.cleaned_data['gateway_name']
                   print gateway_name
            else:
                   add_gateway_details_form=Add_Gateway_Details()
            if add_gateway_parameters_form.is_valid():
                   gateway_param1=add_gateway_parameters_form.cleaned_data['gateway_param1']
                   print gateway_param1
                   gateway_param2=add_gateway_parameters_form.cleaned_data['gateway_param2']
                   print gateway_param2
           else:
                print "else"



  else:
    add_gateway_details_form=Add_Gateway_Details()
    add_gateway_parameters_form=Add_Gateway_Parameters()

当我从add_gateway_parameters_form打印数据时,它会进入else部分但是它对add_gateway_details_form工作正常....(我不是在询问验证)

1 个答案:

答案 0 :(得分:0)

...
if add_gateway_parameters_form.is_valid():
      gateway_param1=add_gateway_parameters_form.cleaned_data['gateway_param1']
      print gateway_param1
      gateway_param2=add_gateway_parameters_form.cleaned_data['gateway_param2']
      print gateway_param2
else:
      print "else"
      #add_gateway_parameters_form=Add_Gateway_Parameters()
      #dont do this, rather just pass `add_gateway_parameters_form` to template

 ...

请勿重置else部分中的表单,只需将表单传递给模板,并在模板中显示表单和表单字段错误。有了它你就可以看出为什么它没有验证。