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