验证并显示自定义Django表单中的错误
当我使用 form.as_p
来显示我的表单时,比如说我点击提交按钮却没有填写任何字段,它会显示错误信息,提醒我填写必填项。不过,当我自定义我的 HTML 模板时,它就不会验证字段,也不会显示任何错误信息。我的 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
我的 HTML 模板是这样的:
# 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>
我现在刚开始学习 Django,我看过关于自定义表单的文档,但没有找到关于如何像 forms.as_p
那样验证字段的信息。
1 个回答
0
你现在的做法是不对的,生成自定义的HTML表单应该用Django提供的表单字段,比如{{ form.first_name }}
这些,而不是自己手动写HTML代码。
不过,问题其实更简单:你忘了在每个字段后面加上{{ form.myfield.errors }}
来显示错误信息,还有在表单顶部加上{{ form.non_field_errors }}
来显示整体的错误信息。
另外,没必要在save
里做那么多额外的工作。那些字段已经由父类处理好了,你只需要手动处理user.set_password
这一部分就可以了。