1

Why form is not valid in django!

hi i dont know why my form is not valid! i tried this

class SignupForm(forms.Form):
    first_name = forms.CharField(required=True, max_length=35)
    last_name = forms.CharField(required=True, max_length=35)
    username = forms.CharField(required=True, max_length=50)
    email = forms.EmailField(required=True)
    password = forms.CharField(required=True, min_length=8, max_length=64)
    confirm_password = forms.CharField(required=True, min_length=8, max_length=64)

template.html

<form id="signupForm" action="" method="POST" accept-charset="utf-8" class="form" role="form">
    {% csrf_token %}
    <div class="row">
        <div class="col-xs-6 col-md-6">
            <input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />
        </div>
        <div class="col-xs-6 col-md-6">
            <input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name"  />
        </div>
    </div>
    <input id="username" type="text" autocomplete="off" name="username" value="" class="form-control input-lg" placeholder="Choose Username" />
    <input id="email" type="text" autocomplete="off" name="email" value="" class="form-control input-lg" placeholder="Your Email" />
    <input id="password" type="password" autocomplete="off" name="password" value="" class="form-control input-lg" placeholder="Password" />
    <input id="confirm_password" type="password" name="confirm_password" value="" class="form-control input-lg" placeholder="Confirm Password" />
    <div class="topmargin active">
        <div class="row" style="display: flex;">
            <div class="col-xs-5 col-md-5">
                <label>I'm </label>
                <select name="month" class = "form-control input-lg">
                    <option value="male">Male</option>
                    <option value="female">Female</option>
                </select>
            </div>
            <br />
            <span class="help-block">By clicking Create my account, you agree to our Terms and that you have read our Data Use Policy, including our Cookie Use.</span>
    <button class="btn btn-block signup-btn" type="submit">Create my account</button>
</form>

i think it's because i dont pass the form to template, but i dont wanna do that! it mixes the responsive style commands are welcome thank you

Community
  • 1
  • 1
Ebrahim Karimi
  • 732
  • 8
  • 24
  • There is no form rendering. You should have. – vishes_shell Jul 11 '17 at 22:32
  • How do you know it is not valid? What's the error? And, yes, it is easier including the form in the template. – jgmh Jul 11 '17 at 22:44
  • all i know is just form.is_valid() returns False @jgmh – Ebrahim Karimi Jul 11 '17 at 22:51
  • Ok. I'd start by printing the `request.POST` in the `views.py` before the validation. That should show you the errors. By the way, shouldn't it be `name="first_name"` instead of `name="firstname"` in order to pass the form to the ``views.py`? – jgmh Jul 11 '17 at 22:56
  • @jgmh i think names doesn't matter! all matters is names should be the same when passing to the form! – Ebrahim Karimi Jul 11 '17 at 23:06

1 Answers1

5

The problems are here:

<input id="first_name" type="text" autocomplete="off" name="firstname" value="" class="form-control input-lg" placeholder="First Name" />

<input id="last_name" type="text" autocomplete="off" name="lastname" value="" class="form-control input-lg" placeholder="Last Name"  />

In your form you use first_name and last_name with underscore, but on input they are without the underscore.

You need to change the input names to match the form fields:

<input id="first_name" type="text" autocomplete="off" name="first_name" value="" class="form-control input-lg" placeholder="First Name" />

<input id="last_name" type="text" autocomplete="off" name="last_name" value="" class="form-control input-lg" placeholder="Last Name"  />
Thiago Rossener
  • 934
  • 1
  • 6
  • 17