2017-10-04 77 views
0

我有一個表單,並且它工作正常。但是當在模型django中添加新字段時提出錯誤總是在Django表單上得到「此字段是必需的」錯誤

當我運行服務器並單擊提交時,它顯示新字段的錯誤該字段是必需的雖然我在表單中爲該字段提供數據。

Model.py

class UserInformation(models.Model): 
firstName     = models.CharField(max_length=128) 
lastName     = models.CharField(max_length=128) 
userName     = models.CharField(max_length=128) 
institution     = models.CharField(choices = [("@xyz.org","XYZ"), ("@abc.edu","ABC")], max_length=128) 
userEmail     = models.CharField(default="N/A", max_length=128) 
phoneNumber     = models.CharField(max_length=128)  
orchidNumber    = models.CharField(max_length=128) 
PI       = models.CharField(max_length=128) 
PIUsername     = models.CharField(max_length=128) 
PIInstitution    = models.CharField(default="N/A",choices = [("@xyz.org","XYZ"), ("@abc.edu","ABC")], max_length=128) 
PIEmail      = models.CharField(default="N/A", max_length=128) 
PIPhoneNumber    = models.CharField(max_length=128) 

在這個模型中

PIEmail is the field which I have added. 

forms.py

class UserInformationForm(ModelForm): 
firstName = forms.CharField(max_length=254, 
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 
lastName = forms.CharField(
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 
userName = forms.CharField(
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 

institution = forms.ChoiceField(choices = [("@xyz.org","XYZ"), ("@abc.edu","ABC")] 
           ,widget=forms.Select({         
           'class': 'form-control', 
           })) 


phoneNumber = forms.CharField(required=False, 
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 
orchidNumber = forms.CharField(required=False, 
          widget=forms.TextInput({ 
           'class': 'form-control', 
           }))         

PI = forms.CharField(
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 
PIUsername = forms.CharField(
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 
ctsaPIInstitution = forms.ChoiceField(choices = [("@xyz.org","XYZ"), ("@abc.edu","ABC")] 
           ,widget=forms.Select({         
           'class': 'form-control', 
           })) 

PIPhoneNumber = forms.CharField(
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 

userEmail = forms.CharField(required=False, 
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 

PIEmail = forms.CharField(required=False, 
          widget=forms.TextInput({ 
           'class': 'form-control', 
           })) 





class Meta: 
    model = UserInformation 
    exclude =() 

,這裏是我的register.html

<div class="row"> 
    <section id="registerForm"> 
     <div style="font-size:15px; color:red;"> 
      The fields marked with an asterisk (*) are mandatory. 
     </div><br/>  
      <form method="post" action=".">{% csrf_token %} 
      <div class="form-group"> 
        <label for="id_firstName" >First Name (*)</label> 
         {{ form.firstName }} 
      </div> 
      <div class="form-group"> 
        <label for="id_lastName" >Last Name (*)</label>      
         {{ form.lastName }}      
      </div> 
      <div class="form-group"> 
        <label for="id_email">Username (*)</label>      
         {{ form.userName }} 
      </div> 
         <div class="form-group"> 
        <label for="id_intitution">Institution (*)</label> 

         {{ form.institution }} 
      </div> 
      <div class="form-group"> 
        <label for="id_phone" >Contact Number</label> 
         {{ form.phoneNumber }} 
      </div> 
      <div class="form-group"> 
        <label for="id_orcid">Orcid ID (<a href="https://orcid.org/register">Get Orcid ID</a>)</label> 
         {{ form.orchidNumber }} 
      </div> 

      <div class="form-group">   
       <label for="id_ctsaPI">Prinicipal Investigator (*)</label>          
       {{ form.PI }} 

      </div> 
     <div class="form-group">   
       <label for="id_PI">CTSA Prinicipal Investigator Username (*)</label>          
       {{ form.PIUsername }} 

      </div> 
     <div class="form-group">   
       <label for="id_ctsaPI">Prinicipal Investigator Institute (*)</label>          
       {{ form.PIInstitution }} 

      </div> 
     <div class="form-group">   
       <label for="id_PIName"> Prinicipal Investigator Phone Number (*)</label>          
       {{ form.PIPhoneNumber }} 

      </div> 

     <div class="form-group">   
       <label for="id_UserEmail">User Email (*)</label>          
       {{ form.userEmail }} 

      </div> 

     <div class="form-group">   
       <label for="id_PI">PI Email (*)</label>          
       {{ form.PIEmail }} 

      </div> 

      <div class="form-group" > 
       <br/> 
       <input type="submit" value="Submit" class="btn btn-primary" /> 

      </div> 

    </form> 
</section> 

view.py

@csrf_protect 
def register(request): 
    if request.method == 'POST': 
     form = UserInformationForm(request.POST) 
     if form.is_valid(): //// here it is breaking 
       form.save() 
    else: 
     form = UserInformationForm() 

    variables = { 'form': form } 

    return render(request, 'registration/register.html',variables) 

我不知道什麼是錯在此代碼

回答

1

我不知道如果這有助於但有時我發現返回的錯誤看起來像是一條紅色的鯡魚,最終讓我瘋狂了好幾個小時。我不是專家,從我坐的地方來看,你的表單的代碼對我來說看起來很好,這可能是以前工作的原因。然而,在你的html文件中,你有兩個標籤用相同的id指定,第二個恰好在你最近添加的PIEmail字段上。巧合?也許!這是一個很長的一步,但也許最初會改變,看看它是否有任何改變。

變化:

<div class="form-group">   
    <label for="id_PI">PI Email (*)</label>          
    {{ form.PIEmail }} 
</div> 

到:

<div class="form-group">   
    <label for="id_PIEmail">PI Email (*)</label>          
    {{ form.PIEmail }} 
</div> 

注:另一個實例是在PIUsername場。

+0

不知道如何,但它這次工作。 –

相關問題