04:form Validating user input & input HTML

Source: Internet
Author: User

The function and basic use of 1.1 form

1, the form of two functions, and the application of the scene

1. function 1: Verify

2. function 2: Generate HTML Tag (default function: Keep last committed value)

3 , new URL mode operation (be sure to use form to generate HTML, to avoid the submission of the Refresh page, missing the current page filled values)

4 , the AJAX request can not generate HTML tags without form, only the form for validation, because the AJAX request itself does not refresh the page, do not have to worry about filling

Value is lost, and it is possible to generate HTML using a form

2, use form to login.html submit password to do simple length verification

 fromDjango.shortcutsImportRender,httpresponse,redirect fromApp01.formsImportUserFormdefLogin (Request):ifRequest.method = ='GET': obj=UserForm ()returnRender (Request,'login.html',{'obj': obj}) elifRequest.method = ='POST': obj=UserForm (Request. POST) R1=obj.is_valid ()ifR1:Print(Obj.cleaned_data)Else:            Print(obj.errors)returnRender (Request,'login.html',{'obj': obj})
defining handler functions in views.py
 fromDjangoImportForms fromDjango.formsImport FieldsclassUserForm (forms. Form):#1: Here the name must be and the value of name in the input boxName =Fields . Charfield (Error_messages={'Required':'user name cannot be empty'},    )    #2: The password here must be the value of name in the input boxPassword =Fields . Charfield (Min_length=6, Max_length=10, Error_messages={'Required':'The password cannot be empty',                        'Min_length':'password length cannot be less than 6',                        'Max_length':'password length cannot be greater than',                        }    )
define field validation rules in app01/forms.py
<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title></title></Head><Body>    <formMethod= "POST"Action= "/login/">        <P>User name:<inputname= "Name"type= "text">{{obj.errors.name.0}}</P>        <P>Password<inputname= "Password"type= "text">{{obj.errors.password.0}}</P>        <P><inputtype= "Submit"value= "Submit"></P>    </form></Body></HTML>
login.html

3. Three ways to generate HTML more simple, but the coupling is too strong, not good customization (not recommended)

1. {obj.as_p}
2. {Obj.as_ul}
3. {Obj.as_table}

04:form Validating user input & input HTML

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.