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.