总是得到"这个字段是必需的" Django表单上的错误

时间:2017-10-04 17:36:23

标签: python 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 个答案:

答案 0 :(得分: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字段。