0

When I use form.as_p to display my form and, for example, I click on Submit button without fill any field, it shows error messages asking me to fill the required fields. However, when I customize the output of my html template, it doesn't validate the fields and doesn't show any error message. My forms.py:

# forms.py
    class SignUpForm(forms.ModelForm):
    username = forms.CharField(label='Username', max_length=75, widget=forms.TextInput(attrs={'placeholder' : 'Username'}))
    email = forms.EmailField(label='Email', max_length=255)
    first_name = forms.CharField(label='First Name', max_length=75)
    last_name = forms.CharField(label='Last Name', max_length=75)
    birthday = forms.DateField(label='Birthday')
    gender = forms.ChoiceField(choices = User.GENDER_CHOICES)

    class Meta:
        model = User
        fields = ['username', 'password', 'email', 'first_name', 'last_name', 'birthday', 'gender']
        widgets = {
            'password': forms.PasswordInput(attrs={'class': 'form-control'}),
            'email': forms.EmailInput(attrs={'class': 'form-control'}),
            'birthday': forms.DateInput(attrs={'class': 'form-control'}),
        }

    def save(self, commit=True):
        user = super(SignUpForm, self).save(commit=False)
        user.username = self.cleaned_data['username']
        user.set_password(self.cleaned_data['password'])
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.birthday = self.cleaned_data['birthday']
        user.gender = self.cleaned_data['gender']
        if commit:
            user.save()
        return user

My HTML template is like this:

# registration.html
<form class="form-signin" action="/register/" method="post">
    <h2 class="form-signin-heading">registration now</h2>
    <div class="login-wrap">
        <p>Enter your personal details below</p>
        <input type="text" class="form-control" placeholder="{{ form.first_name.label }}" name="{{ form.first_name.name }}" id="id_{{ form.first_name.name }}" maxlength="75" autofocus>
        <input type="text" class="form-control" placeholder="{{ form.last_name.label }}" name="{{ form.last_name.name }}" id="id_{{ form.last_name.name }}" maxlength="75" autofocus>
        <input type="text" class="form-control" placeholder="{{ form.email.label }}" name="{{ form.email.name }}" id="id_{{ form.email.name }}" maxlength="255" autofocus>

        <div class="radios">
            {% for choice in form.gender.field.choices %}
                <label class="label_radio col-lg-4 col-sm-4" for="">
                    <input name="{{ form.gender.name }}" id="radio-01" value="{{choice.0}}" type="radio" checked /> {{ choice.1 }}
                </label>
            {% endfor %}
        </div>

        <p> Enter your account details below</p>
        <input type="text" class="form-control" placeholder="{{ form.username.label }}" name="{{ form.username.name }}" id="id_{{ form.username.name }}" autofocus>
        <input type="password" class="form-control" placeholder="{{ form.password.label }}" name="{{ form.password.name }}" id="id_{{ form.password.name }}">
        <label class="checkbox">
            <input type="checkbox" value="agree this condition"> I agree to the Terms of Service and Privacy Policy
        </label>
        <input class="btn btn-lg btn-login btn-block" type="submit" value="Submit"/>

        <div class="registration">
            Already Registered.
            <a class="" href="login.html">
                Login
            </a>
        </div>
    </div>
  </form>

I'm starting now with Django, I read the documentation about custom forms, but I didn't find nothing about how to validate fields as forms.as_p does.

Alexandre Lara
  • 2,464
  • 1
  • 28
  • 35

1 Answers1

0

That is the wrong way to produce custom HTML for your form. You should still use the Django form fields - {{ form.first_name }} etc - rather than creating HTML versions yourself and populating them.

However, the issue is simpler than that: you have forgotten to add the {{ form.myfield.errors }} for each field, and the {{ form.non_field_errors }} at the top of the form.

Also note there's no reason to do all that extra work in save. All of those fields are already being set by the superclass: the only thing you need to take care of manually is user.set_password.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • What would be the right way? I had to create something like this? `username = forms.CharField(label='Username', max_length=75, widget=forms.TextInput(attrs={'placeholder' : 'Username', 'class' : 'form-control'}))`. – Alexandre Lara Sep 12 '14 at 08:58
  • Yes, something like that. Or see [this question](http://stackoverflow.com/questions/4101258/how-do-i-add-a-placeholder-on-a-charfield-in-django) for various ideas on doing it dynamically. – Daniel Roseman Sep 12 '14 at 09:08