Python 是否可以自定义适用于某些注册错误的django.auth错误消息?

Python 是否可以自定义适用于某些注册错误的django.auth错误消息?,python,django,Python,Django,我试图为用户在注册时发生的某个错误显示一条错误消息,因为现在我只讨论了一个关于密码和conf_passowrd不匹配的错误,但只有该场景是有效的,只要出现一个关于输入的密码太短的错误,它仍然显示有关密码不匹配的错误 到目前为止,我所尝试的: view.py: from django.shortcuts import render, redirect from django.contrib import messages from django.contrib.auth.forms import

我试图为用户在注册时发生的某个错误显示一条错误消息,因为现在我只讨论了一个关于密码和conf_passowrd不匹配的错误,但只有该场景是有效的,只要出现一个关于输入的密码太短的错误,它仍然显示有关密码不匹配的错误

到目前为止,我所尝试的:

view.py:

from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.forms import UserCreationForm


def indexsignup(request):
    form = UserCreationForm()
    if request.method == 'POST':
        regForm = UserCreationForm(request.POST)
        if regForm.is_valid():
            regForm.save()
            return redirect('login')
        else:
            for msg in form.error_messages:
                if len('password1') < 6:
                    messages.error(request, f"PAss short")
                    print(msg)
                else:
                    messages.error(request, f"Two passwords don't match.")
                    print(msg)     
    return render(request, 'registration/register.html', {'form':form})
从django.shortcuts导入渲染,重定向
从django.contrib导入消息
从django.contrib.auth.forms导入UserCreationForm
def indexsignup(请求):
form=UserCreationForm()
如果request.method==“POST”:
regForm=UserCreationForm(request.POST)
如果regForm.is_有效():
regForm.save()
返回重定向('登录')
其他:
对于form.error_消息中的消息:
如果len('password1')<6:
消息。错误(请求,f“短传递”)
打印(msg)
其他:
messages.error(请求,f“两个密码不匹配”)
打印(msg)
返回呈现(请求'registration/register.html',{'form':form})
register.html:

{% load static %}

<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>Forum - SignUp</title>
    <link rel="stylesheet" href="{% static 'signup/style.css' %}">
</head>

<body>
    <div class="signin-box">
        <h1> Sign Up</h1>
        <form method="post" action="">
            {% csrf_token %}
            <div class="textbox">
                <input type="text" name="username" placeholder="Username" maxlength="10" autocapitalize="none"
                    autocomplete="username" autofocus="" required="" id="id_username">
            </div>
            <div class="textbox">
                <input type="password" name="password1" placeholder="Password" autocomplete="new-password" required=""
                    id="id_password1">
                <script type="text/javascript">
                    function reveal() {
                        if (document.getElementById('box').checked) { document.getElementById("id_password1").type = 'text'; }
                        else
                            document.getElementById("id_password1").type = 'password';
                    }
                </script>
                <div class="check">
                    <input title="Reveal password" type="checkbox" id="box" onclick="reveal()">
                </div>
            </div>
            <div class="textbox">
                <input type="password" name="password2" placeholder="Confirm Password" autocomplete="new-password"
                    required="" id="id_password2">
                    <script type="text/javascript">
                        function reveal2() {
                            if (document.getElementById('box').checked) { document.getElementById("id_password2").type = 'text'; }
                            else
                                document.getElementById("id_password2").type = 'password';
                        }
                    </script>
                    <div class="check">
                        <input title="Reveal password" type="checkbox" id="box" onclick="reveal2()">
                    </div>
            </div>
            {% if messages %}
            {% for message in messages %}
            <div class="help">
                <p><span style="font-size: 20px">&#9888;</span> {{message}}</p>
            </div>
            {% endfor %}
            {% endif %}
            <input class="btn" type="submit" style="opacity: 1 !important;" value="Sign Up">
        </form>
    </div>
</body>
{%load static%}
论坛-注册
注册
{%csrf_令牌%}
函数揭示(){
如果(document.getElementById('box').checked){document.getElementById(“id_password1”).type='text';}
其他的
document.getElementById(“id_password1”).type='password';
}
函数reveal2(){
如果(document.getElementById('box').checked){document.getElementById(“id_password2”).type='text';}
其他的
document.getElementById(“id_password2”).type='password';
}
{%if消息%}
{消息%中的消息为%s}
⚠;{{message}}

{%endfor%} {%endif%}
最好是以django格式添加
ValidationError
。您需要为
password1
字段(或要添加自定义项的任何字段)重写
clean
方法。在
UserCreationForm
类中添加以下函数

def clean_password1(self):
    entered_password = self.cleaned_data['password1']
    if len(entered_password) <= 6:
        raise forms.ValidationError("Password should include more than 6 characters")
    return entered_password
def clean_密码1(self):
输入的\u密码=self.cleanned\u数据['password1']
if len(输入密码)