开发者

How to override password_change error messages in django?

I have spent ages on the net how to override the change_password form in django. I created my forms and I can change my passwords successfully. Now I want to display my errors on the page. (By the way , I'm using django-registration.)

The problem is I have to change error messages. In my template I'm using this function to display new_password1 errors :

{% if form.new_password1.errors %}
     <div class="error_message"> blah </div>
{% endif %}

However it consists of all new_password1 errrors. I looked the docs and found that CharField has only reqiured, max and min. I need to check each specific errors of the new_password1 (confirmation error, required error and min,max) and produce my own error messages. Since the CharField doesn't have a matching keyword, I tried a clean method in my forms.py which looks like :

 def clean_password2(self):                                                                                                                                                                                                                               
        if  'new_password1' in self.cleaned_data and 'new_password2' in self.cleaned_data:                                                                                                                                                                   
            if self.cleaned_data['new_password1'] != self.cleaned_data['new_password2']:                                                                                                                                                              开发者_Go百科       
                raise forms.ValidationError(_(u"no match"))                                                                                                                                                                                                

        return cleaned_data   

Finally, I don't know how to check this clean_password2 validation in my template. When I wrote registration page, I can display all my errors by looping the errors. And it displays all overrided errors. This time it doesn't work. So my quesstions are:

1- How can i check all specific errors in my templates ? like if form.new_password1.required.errors

2- How can i display this clean_password2 messages ?


In django-speak, what you're referring to as the view is the template. It's rather confusing!

Update: I see that you define a clean_password2 function but you are checking for fields called new_password1, new_password2.

Unless you have a field called password2, that is the field that your clean method is validating.

The messages for clean_password2 are generated in {{ form.password2.errors }}, but to me, it sounds like you don't have a field called password2.

Change the name of your method to clean_new_password2.

{% if form.new_password1.errors %}{{ form.new_password1.errors }}{% endif %} 
{% if form.new_password2.errors %}{{ form.new_password2.errors }}{% endif %}

Form field cleaning works per field.. you access the errors off the field object.


Like @Yuji said, use {% if form.non_field_errors %} in your template.

Now just a bit on usability. Concerning the user registration and password change form, the only non-field errors your can expect will be mismatching passwords, so unlike other forms where your going to print the non-field errors at the very beginning, for these forms it makes sense to print the one non-field error before the two password input fields, possibly wrapping them in a parent that has a red background, to make it more obvious that the values of the two fields are related and that the error concerns the two fields, not the form in general.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜