2012-07-05 168 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