2017-10-04 3 views
0

J'ai eu un formulaire avec quelques champs et il fonctionnait bien. Mais lors de l'ajout de nouveaux champs dans le modèle django soulever une erreurtoujours obtenir "Ce champ est requis" erreur sur Django formulaire

quand je lance le serveur et cliquez sur le transmettrons indique l'erreur pour le nouveau champ Ce champ est obligatoire bien que je fournirai des données pour ce domaine sous la forme .

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) 

Dans ce modèle

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

et voici mon 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) 

Je ne sais pas ce qui ne va pas dans ce code

Répondre

1

Je ne sais pas si cela aide, mais parfois je trouve les erreurs retournées ressemblent à un peu de hareng rouge et finissent par me rendre fou pendant des heures. Je ne suis pas un expert et d'où je suis assis le code pour votre formulaire me semble bien, qui est probablement pourquoi il travaillait avant. Cependant, dans votre fichier html, vous avez deux étiquettes spécifiées avec le même identifiant, la seconde se trouve sur le champ PIEmail que vous avez récemment ajouté. Coïncidence? Peut être! C'est un coup long, mais peut-être changer cela au départ et voir si cela fait une différence.

Change:

<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> 

Note: L'autre exemple est sur le terrain PIUsername.

+0

ne sais pas comment, mais cela a fonctionné cette fois. –