Click here to Skip to main content
15,881,173 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I built a simple signup form which will take data as user will click submit it will save it in database. But for some reason I am able to validate it. I removed all the validations and added again too. Not sure where I am missing?

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:
<form action="#" class="p-4 border rounded" method = 'POST'>

              {% csrf_token %}

              <div class="row form-group">
                <div class="col-md-12 mb-3 mb-md-0">
                  <label class="text-black" 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 class="row form-group">
                <div class="col-md-12 mb-3 mb-md-0">
                  <label class="text-black" 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 class="row form-group mb-4">
                <div class="col-md-12 mb-3 mb-md-0">
                  <label class="text-black" 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 class="row form-group">
                <div class="col-md-12 mb-3 mb-md-0">
                  <label class="text-black" 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 class="row form-group">
                  <div class="col-md-12 mb-3 mb-md-0">
                    <label class="text-black" 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 class="row form-group">
                <div class="col-md-12">
                  <input type="submit" value="Sign Up" class="btn px-4 btn-primary text-white">
                </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.
Posted
Updated 3-Jul-22 2:36am
v3
Comments
Member 15678123 3-Jul-22 0:27am    
what do you mean by "not validating" the only validation I see here the fields are required?


also can you include
UserCreationForm
Kavya Bhargava 3-Jul-22 5:00am    
I have included user creation form in this part- class RegisterForm(UserCreationForm):
Kavya Bhargava 3-Jul-22 5:12am    
Not validating means it is not saving and giving the error Error Processing Your Request. which is in the else block of the code
Member 15678123 3-Jul-22 5:37am    
if it firing else then that means the form you are filling is not valid, you can check the errors using {{form.errors}}
Kavya Bhargava 3-Jul-22 8:34am    
yes I have added that. Not getting anything

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900