I. Learning content and Induction
1 " "2 Form Components3 -Validation of user requests4 -Form5 -Ajax6 -Generate HTML code7 8 A. Create a class9 B. Creating a corresponding field in a class (including regular expressions)Ten c. GET One -obj = Fr () generates HTML code A d. POST - -obj = Fr (Request. POST) - -If Obj.is_valid (): # To determine if validation is successful the Obj.cleaned_data - Else: - obj.errors - " "
Second, create the class
1 #creating classes, corresponding fields2 fromDjangoImportForms3 fromDjango.formsImport Fields4 classF1form (forms. Form):5user = fields. Charfield (required=True,6Min_length=6,7Max_length=32,8error_messages={9 'Required':'user name cannot be empty',Ten 'Min-length':'Too Short', One 'Invalid':'Format Error' #format errors are used invalid A } - ) -PWD = fields. Charfield (required=True, theMin_length=20, -error_messages={ - 'Required':'The password cannot be empty' - } + ) -Age = fields. Integerfield (required=True) +email = fields. Emailfield (Required=true)
Third, views.py operation
1 defF1 (Request):2 ifRequest.method = ="GET":3 #Generate HTML4obj =F1form ()5 returnRender (Request,'f1.html',{'obj': obj})6 Else:7 #u = Request. Post.get ("user") # non-empty, length 6 to8 #p = Request. Post.get ("pwd") # Non-empty, greater than9 #a = Request. Post.get ("Age") # Non-empty, integerTen #e = Request. Post.get ("email") # non-empty, mailbox format One #print (u,p,a,e) Aobj =F1form (Request. POST) - #Obj.is_valid () whether all validation is successful - ifobj.is_valid (): the #user-Submitted data - Print(Obj.cleaned_data) - returnredirect'http://www.xiaohuar.com') - Else: + #validation error message - Print(obj.errors) + returnRender (Request,'f1.html', {'obj': obj})
Four, HTML operation
1 <formAction= "/f1.html"Method= "POST">2 <P>{{Obj.user}} {{obj.errors.user.0}}</P>3 <P>{{OBJ.PWD}} {{obj.errors.pwd.0}}</P>4 <P>{{Obj.age}} {{obj.errors.age.0}}</P>5 <P>{{Obj.email}} {{obj.errors.email.0}}</P>6 <P><inputtype= "Submit"value= "Submit"></P>7 </form>
West Tour Road--python the form component of full stack--django