Mohammad Shahbaz
Mohammad Shahbaz

Reputation: 423

always getting "This field is required" error on Django form

I had a form with some fields and it was working fine. But when adding new field in the Model django raise an error

when I run the server and click on submit then it shows error for the new field This field is required although I am providing data for this field in the form.

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)

In this model

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 = ()

and here is my 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)

I am not sure what is wrong in this code

Upvotes: 0

Views: 4846

Answers (1)

cander
cander

Reputation: 289

I'm not sure if this helps but sometimes I find the errors returned look like a bit of a red herring and end up driving me mad for hours on end. I am no expert and from where I am sitting the code for your form looks fine to me which is probably why it was working before. However in your html file you have two labels specified with the same id, the second one just happens to be on the PIEmail field that you have recently added. Coincidence? Maybe! It's a long shot but perhaps change that initially and see if it makes any difference.

Change:

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

to:

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

Note: The other instance is on the PIUsername field.

Upvotes: 1

Related Questions