Django failed to get cleaned_data attribute

The cleaned_data attribute is only available after the clean () method is called. If a field cleaning function is defined using the clean_field name method in form validation, django will call this function first, and finally call the clean () function, as follows:

def clean_mobile (self): 
  mobile = self.cleaned_data ['mobile'] #Write like this is wrong, then the cleaned_data attribute is not generated

 

  Calling the cleaning function, the essence is to convert the form type to python data type. If there are two fields in the form, namely mobile and password, the clean_mobile cleaning field mobile is defined in the form class (that is, the field mobile is completed by the clean_mobile () method. Convert from the form type to the python data type), then the field password will be cleaned by the built-in clean () method, and converted to the python type (that is, only the password field in the cleaned_data attribute can be obtained through login_form.cleaned_data ['password'] ). The data attribute contains all the field values ​​of the form, as follows:

 

Correct example:

       

 

   

       

 

 

  The above code just tells the reader that the data attribute can be used to get the form data. You can also use the form: register_post_form.password.value to get form data at the front end.

  

  Note: Readers can use the breakpoint method to view the fields contained in the form object.

 

Guess you like

Origin www.cnblogs.com/xiaohaodeboke/p/12757934.html