[Oldboy-django] [2 in-depth Django] initial form component

Source: Internet
Author: User

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

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.