Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django FormView和表单验证问题_Django - Fatal编程技术网

Django FormView和表单验证问题

Django FormView和表单验证问题,django,Django,我的问题是,即使我输入了两个不同的密码,也不会显示错误消息,但如果我输入的密码少于6个字符,则会显示错误消息 以下是我的看法: class Registration(FormView): template_name = 'accounts/registration.html' form_class = RegistrationForm success_url = reverse_lazy('accounts:index') def form_valid(self, form

我的问题是,即使我输入了两个不同的密码,也不会显示错误消息,但如果我输入的密码少于6个字符,则会显示错误消息

以下是我的看法:

class Registration(FormView):
   template_name = 'accounts/registration.html'
   form_class = RegistrationForm
   success_url = reverse_lazy('accounts:index')

def form_valid(self, form):
    # my stuff...

即使2个密码不同,而不仅仅是一个密码少于6个字符时,我如何才能强制进行这种“弹出”操作?

[UPDATE]:问题似乎出在HTML模板中。OP所要做的就是将
{{form.errors}}
放入模板中,以便显示
验证错误

def clean(self):
    cleaned_data = super().clean()  # call the super clean() method first

    password = cleaned_data.get('password')
    confirm_password = cleaned_data.get('confirm_password')

    if password and confirm_password:
        if password != confirm_password:
            raise ValidationError("Password error")

来源:

[UPDATE]:问题似乎出在HTML模板中。OP所要做的就是将
{{form.errors}}
放入模板中,以便显示
验证错误

def clean(self):
    cleaned_data = super().clean()  # call the super clean() method first

    password = cleaned_data.get('password')
    confirm_password = cleaned_data.get('confirm_password')

    if password and confirm_password:
        if password != confirm_password:
            raise ValidationError("Password error")

资料来源:

请在
if
子句前面放一个
打印
语句,同时打印
密码
确认密码
。你应该把它贴出来才能弄清楚!打印语句是怎么说的?它打印我输入的两个密码,因为我更新了答案。奇怪的是,它没有验证。您确实输入了不同的密码来强制显示
ValidationError
,对吗?是的,我正在尝试这样做。但是它没有出现,即使使用您提出的新解决方案,请在
if
子句之前放置一个
print
语句,并同时打印
password
confirm\u password
。您应该发布它,以便找出答案!打印语句是怎么说的?它打印我输入的两个密码,因为我更新了答案。奇怪的是,它没有验证。您确实输入了不同的密码来强制显示
ValidationError
,对吗?是的,我正在尝试这样做。但它并没有出现,即使你提出了新的解决方案