West Tour Road--python the form component of full stack--django

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.