2012-07-05 73 views
0

我看来is--表单验证工作不

def create_account(request): 

    if request.method == 'POST': 
     form = CreateAccountForm(request.POST) 
     if form.is_valid(): 

      acc_act_date = form.cleaned_data['Account_Activation_Date'] 
      present_date = date.today() 
      if acc_act_date <= present_date: 
       stat = 'Active' 
      else: 
       stat = 'Inactive' 

      a = Account_status.objects.get(status = stat) 


      sto = Create_account_tab(account_number=form.cleaned_data['Account_Number'], 
       account_name=form.cleaned_data['Account_Name'], 
       account_description=form.cleaned_data['Account_Description'], 
       account_status_key=a, 
       account_manager=form.cleaned_data['Account_Manager'], 
       parent_account_number=form.cleaned_data['Parent_Account_Number'], 
       account_activation_date=form.cleaned_data['Account_Activation_Date'], 
       ) 
      sto.save() 

      return HttpResponseRedirect('/create_account/thanks/') 
    else: 
     form = CreateAccountForm() 
     return render_to_response('CreateAccountForm.html', {'form': form}, context_instance=RequestContext(request)) 

和我的形式is--

class CreateAccountForm(forms.Form): 
    Account_Number = forms.CharField() 
    Account_Name = forms.CharField() 
    Account_Description = forms.CharField() 
    Account_Manager = forms.CharField() 
    Parent_Account_Number = forms.CharField(required = False) 
    Account_Activation_Date = forms.DateField() 

和我的模板is--

<html> 
<head> 
    <title>Your Information</title> 
</head> 
<body> 
    <h1>Fill in your details</h1> 

{% if form.errors %} 
    <p style="color: red;"> 
     Please correct the error{{ form.errors|pluralize }} below. 
    </p> 
{% endif %} 

<form action="." method="post">{% csrf_token %} 
    <div class="field"> 
     {{ form.Account_Number.errors }} 
     <label for="id_Account_Number">Account Number:</label> 
     {{ form.Account_Number }} 
    </div> 
    <div class="field"> 
     {{ form.Account_Name.errors }} 
     <label for="id_Account_Name">Account Name:</label> 
     {{ form.Account_Name }} 
    </div> 
    <div class="field"> 
     {{ form.Account_Description.errors }} 
     <label for="id_Account_Description">Account Description:</label> 
     {{ form.Account_Description }} 
    </div> 
    <div class="field"> 
     {{ form.Account_Manager.errors }} 
     <label for="id_Account_Manager">Account Manager:</label> 
     {{ form.Account_Manager }} 
    </div> 
    <div class="field"> 
     {{ form.Parent_Account_Number.errors }} 
     <label for="id_Parent_Account_Number">Parent Account Number:</label> 
     {{ form.Parent_Account_Number }} 
    </div> 
    <div class="field"> 
     {{ form.Account_Activation_Date.errors }} 
     <label for="id_Account_Activation_Date">Account Activation Date:</label> 
     {{ form.Account_Activation_Date }} 
    </div> 
    <input type="submit" value="Submit"> 
</form> 

现在的问题是,我在模板中完成的验证不再工作,当我生成一个错误时,它显示django错误视图account.views.create_account没有返回HttpResponse对象。而不是我想从模板中显示的错误。 :( 谁能帮我这??

回答

1

您的视图返回None如果request.method == 'POST'form.is_valid()是假。

取消缩进视图的最后一行逻辑恢复到应该如何

+0

thanku !!这让我很尴尬..:\ – nimeshkiranverma 2012-07-05 11:45:49