如何覆盖 django 中的password_change错误消息?

发布于 2024-10-22 04:05:44 字数 1754 浏览 1 评论 0原文

我花了很长时间在网上如何覆盖 django 中的change_password 表单。我创建了表单,并且可以成功更改密码。现在我想在页面上显示我的错误。 (顺便说一句,我正在使用 django-registration。)

问题是我必须更改错误消息。在我的模板中,我使用此函数来显示 new_password1 错误:

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

但是它包含所有 new_password1 错误。我查看了文档,发现 CharField 仅需要 max 和 min。我需要检查 new_password1 的每个特定错误(确认错误、必需错误和最小值、最大值)并生成我自己的错误消息。由于 CharField 没有匹配的关键字,我在 forms.py 中尝试了一个干净的方法,如下所示:

 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']:                                                                                                                                                                     
                raise forms.ValidationError(_(u"no match"))                                                                                                                                                                                                

        return cleaned_data   

最后,我不知道如何在模板中检查 clean_password2 验证。当我编写注册页面时,我可以通过循环错误来显示我的所有错误。它显示所有被覆盖的错误。这次不行了。所以我的问题是:

1-如何检查模板中的所有特定错误?就像 if form.new_password1.required.errors

2-我如何显示此 clean_password2 消息?

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']:                                                                                                                                                                     
                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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拔了角的鹿 2024-10-29 04:05:44

在 django 语言中,您所指的视图就是模板。真是比较混乱啊!

更新:我看到您定义了一个 clean_password2 函数,但您正在检查名为 new_password1new_password2 的字段。

除非您有一个名为 password2 的字段,否则 that 就是您的 clean 方法正在验证的字段。

clean_password2 的消息是在 {{ form.password2.errors }} 中生成的,但对我来说,听起来您没有名为 password2 的字段

将方法名称更改为 clean_new_password2

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

表单字段清理按字段进行。您可以访问字段对象的错误。

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.

时光礼记 2024-10-29 04:05:44

就像@Yuji所说,在模板中使用 {% if form.non_field_errors %}

现在简单介绍一下可用性。关于用户注册和密码更改表单,您可以预期的唯一非字段错误将是密码不匹配,因此与您要在一开始打印非字段错误的其他表单不同,对于这些表单,打印是有意义的两个密码输入字段之前的一个非字段错误,可能将它们包装在具有红色背景的父字段中,以便更明显地表明这两个字段的值是相关的,并且错误涉及这两个字段,而不是形式一般。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文