Http://www.cnblogs.com/wupeiqi/articles/6144178.html
1 Initial Form Component
#form validation (initial Form component validation)-Questions:-Unable to remember last commit, page refresh data disappears-verification of repeated commit data (whether the data is empty, length size, etc.)-Workaround: Django Form Components-define rules (the format of the data, the fields must match the Name property on the form)classLoginForm (Form): Instantiating object obj=LoginForm (Request. POST)-Data Inspection obj.is_valid ()-provide detailed error information (can be customized error prompt)--custom error messages, which are passed through the Error_messages parameter when the class is defined obj.errors-provides information that conforms to the rules (type is dictionary, database ORM operation support type is dictionary operation) Obj.cleaned_data-Form instance: A. Defining rules fromDjango.formsImportForm, fieldsclassLoginForm (Form):#define rules, fields are regular validation #Usernma and password must match the name of input in the front-end template formUsername = fields. Charfield (Required=true, max_length=16, min_length=6, Error_messages={ 'Required':'cannot be empty', 'Max_length':'length must be less than', 'Min_length':'length must be greater than 6'}) Password= fields. Charfield (Required=true, min_length=8, Error_messages={ 'Required':'cannot be empty', 'Min_length':'length must be greater than 8'}) b. UsingdefLogin (Request):ifRequest.method = ='GET': returnRender (Request,'login.html') Else: obj=LoginForm (Request. POST)#Verify that the submission data conforms to the rules ifobj.is_valid ():Print(Obj.cleaned_data)#Obj.cleaned_data is a dictionary that forms the data submitted by a form #{' password ': ' aaaaaaaaaa ', ' username ': ' Alexadfdda '} returnredirect'http://www.baidu.com') Else: returnRender (Request,'login.html', {'Error': obj.errors})#Job (login, register) final version-keep the last entered value-Validation of user data formats
View Code
[Oldboy-django] [2 in-depth Django] initial form component