Home > other >  Unable to save my form field getting error again and again
Unable to save my form field getting error again and again

Time:07-04

I built a simple signup form that will take data as the user will click submit it and will save it in the database. But for some reason, I am able to validate it. I removed all the validations and added them again too. Not sure where I am missing? it is not saving and giving the error Error Processing Your Request. which is in the else block of the code

Forms.py:

class RegisterForm(UserCreationForm):
    email = forms.EmailField(max_length=100,help_text='Enter Email Address', required=True,widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Email'}))
    
    first_name = forms.CharField(max_length=100,help_text='Enter First Name', required=True,widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'First Name'}))
    
    last_name = forms.CharField(max_length=100,help_text='Enter Last Name', required=True,widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Last Name'}))
    
    username = forms.CharField(max_length=100,help_text='Enter Username', required=True,widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Username'}))
    
    password = forms.CharField(max_length=200,help_text='Enter Password', required=True,widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}))
    
    password2 = forms.CharField(max_length=200,help_text='Enter your Password again', required=True,widget=forms.PasswordInput(attrs={'class': 'form-control', 'placeholder': 'Password'}))  
    
    #check = forms.BooleanField(required = True)
    
    class Meta:
        model = User
        fields = [
            'username', 'email', 'first_name', 'last_name', 'password', 'password2' #, 'check'
        ]

Views.py:

def register(request):
    if request.method == 'GET': #when you request a form it is in a get request 
        form = RegisterForm()
        context = {'form' : form} #this creates a object which you want to send to the front end
        return render(request, 'signup-login.html', context) # here we pushing that form to the html page 
    
    if request.method == 'POST': #when you submit a form it is POST request
        form = RegisterForm(request.POST)
        if form.is_valid():
             form.save()
             user = form.cleaned_data.get('username')
             messages.success(request, 'Account was created for'   user)
             return redirect('home_page')
        else:
            messages.error(request, 'Error Processing Your Request')
            context = {'form' : form}
            return render(request, 'signup-login.html', context)

    return render(request, 'signup-login.html', {})

form HTML(added just the HTML part):

<form action="#"  method = 'POST'>

              {% csrf_token %}

              <div >
                <div >
                  <label  for="fname">Email</label>
                  {{ form.email }} {% comment %}removed the input and added this do for all {% endcomment %}
                </div>
                {% if form.email.errors %}
                {% for error in form.email.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %} {% comment %}   do this for all  {% endcomment %}
              </div>
              <div >
                <div >
                  <label  for="fname">Password</label>
                  {{ form.password }}
                </div>
                {% if form.email.password %}
                {% for error in form.password.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}
                
              </div>
              <div >
                <div >
                  <label  for="fname">Re-Type Password</label>
                  {{ form.password2 }}
                </div>
                {% if form.password2.errors %}
                {% for error in form.password.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}
              </div>

              <div >
                <div >
                  <label  for="fname">First Name</label>
                  {{ form.first_name}}
                </div>
                {% if form.first_name.errors %}
                {% for error in form.first_name.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}
              </div>

              <div >
                  <div >
                    <label  for="fname">Last Name</label>
                    {{ form.last_name}}
                  </div>
                  {% if form.last_name.errors %}
                {% for error in form.last_name.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}
                </div>

              <div >
                <div >
                  <input type="submit" value="Sign Up" >
                </div>
              </div>     

            </form>

The code is displaying so I am assuming there is no error in the html side of it

What I have tried:

I tried several different values again and again but it is not validating.

Thank you so much for helping!!

CodePudding user response:

Updated Answer

Try adding the non-field-errors to your form HTML template and check if there is any message printed.

<form action="."  method="POST">
    {% csrf_token %}
    {{ form.non_field_errors }}
    <!-- DEBUG -->
    {% for field in form %}
        {% if field.errors %}{{ field.html_name }}: {{ field.errors }}{% endif %}
    {% endfor %}
    <!-- DEBUG -->
    <!-- ... your markup ... -->
</form>

Docs: https://docs.djangoproject.com/en/4.0/topics/forms/#looping-over-the-form-s-fields

Error in your markup

There is a error in your template code. You are checking for the errors of password2 but printing only the errors for password

                {% if form.password2.errors %}
                {% for error in form.password.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}

should be

                {% if form.password2.errors %}
                {% for error in form.password2.errors %}
                        <div class "alert">
                           {{ error|escape }}
                        </div>
                {% endfor %}
                {% endif %}
  • Related