在django中显示密码验证错误

时间:2011-03-13 09:37:44

标签: django validation templates

我正在尝试验证我的注册表单。我正在使用djanga-registration,它已经具有密码验证功能。

在我的注册表格中,我正在使用此功能:

{% if form.errors %}
   {% for field in form %}
      <div class="error_message">
           {{ field.errors }}
      </div>
   {% endfor %}
{% endif %}

它显示除密码匹配验证之外的所有错误:

def clean(self):
    """                                                                                                                                                                                                                                                  
    Verifiy that the values entered into the two password fields                                                                                                                                                                                         
    match. Note that an error here will end up in                                                                                                                                                                                                        
    ``non_field_errors()`` because it doesn't apply to a single                                                                                                                                                                                          
    field.                                                                                                                                                                                                                                               

    """
    if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
        if self.cleaned_data['password1'] != self.cleaned_data['password2']:
        raise forms.ValidationError(_(u'no no no'))

    return self.cleaned_data

在我的另一个视图中,更改密码,它显示密码匹配验证,但不是我写的那个(“不不不”),我认为它显示默认密码。

那么任何想法?

1 个答案:

答案 0 :(得分:5)

看起来您正确实现了视图,但您的模板没有:

{{ form.non_field_errors }}

http://docs.djangoproject.com/en/dev/topics/forms/#form-objects

"""
Verifiy that the values entered into the two password fields
match. Note that an error here will end up in
``non_field_errors()`` because it doesn't apply to a single
field.

"""