在自定义Django表单中验证和显示错误

时间:2014-09-12 06:56:21

标签: python django forms validation

当我使用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 :(得分:0)

这是为表单生成自定义HTML的错误方法。你仍然应该使用Django表单字段 - {{ form.first_name }}等 - 而不是自己创建HTML版本并填充它们。

但是,问题比这简单:您忘记为每个字段添加{{ form.myfield.errors }},并在表单顶部添加{{ form.non_field_errors }}

另请注意,没有理由在save中完成所有额外工作。所有这些字段都已由超类设置:您需要手动处理的唯一事项是user.set_password