问题1:  注册页面输入为空,报错:keyError:找不到password

def clean(self):

        print("---",self.cleaned_data)
        #  if self.cleaned_data["password"]==self.cleaned_data["repeat_password"]:        
        #  报错原因:self.cleaned_data是干净数据,如果页面没有输入内容,则self.cleaned_data没有password。
        改如下:
        if self.cleaned_data.get("password")==self.cleaned_data.get("repeat_password"):
            return self.cleaned_data
        else:
            raise ValidationError("两次密码不一致")

 

 2  为什么要用全局clean():

Django_form补充

Django_form补充

Django_form补充

Django_form补充

按子段顺序一一校验,即校验到username时,你无法使用self.cleaned_data.get("password")。

而局部钩子使用完,到全局时,已经可以使用所有的self.cleaned_data

3

Django_form补充

 

 
Django系列学习

问题1:  注册页面输入为空,报错:keyError:找不到password

def clean(self):

        print("---",self.cleaned_data)
        #  if self.cleaned_data["password"]==self.cleaned_data["repeat_password"]:        
        #  报错原因:self.cleaned_data是干净数据,如果页面没有输入内容,则self.cleaned_data没有password。
        改如下:
        if self.cleaned_data.get("password")==self.cleaned_data.get("repeat_password"):
            return self.cleaned_data
        else:
            raise ValidationError("两次密码不一致")

 

 2  为什么要用全局clean():

Django_form补充

Django_form补充

Django_form补充

Django_form补充

按子段顺序一一校验,即校验到username时,你无法使用self.cleaned_data.get("password")。

而局部钩子使用完,到全局时,已经可以使用所有的self.cleaned_data

3

Django_form补充

 

相关文章:

  • 2021-04-02
  • 2021-09-03
  • 2021-06-24
  • 2021-05-06
  • 2021-07-14
  • 2021-06-13
  • 2021-10-22
  • 2021-12-12
猜你喜欢
  • 2021-06-06
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-06-01
  • 2021-05-02
相关资源
相似解决方案