Code in views.py:
1 fromDjango.httpImportHttpResponse2 fromDjango.shortcutsImportRender_to_response3 fromDjangoImportForms4 5 classUserForm (forms. Form):6Name =forms. Charfield ()7 8 defRegister (req):9 ifReq.method = ="POST":Ten #Binding of Form objects Oneform =UserForm (req. POST) A #Validation of data - ifform.is_valid (): - #access to Data the PrintForm.cleaned_data - returnHttpResponse ("OK") - Else: - #Build Object +form =UserForm () - returnRender_to_response ("index.html",{"form": Form})
Code in Index.html:
1 < method= "POST">2 {{form}}3 < type = "submit" value = "OK" > 4 </form>
When the first visit is a "GET" method, generate the corresponding form object, rendering the interface; When the OK button is clicked, the corresponding "POST" method is the binding of the Form object, the validation of the data, and some other column actions.
Simple application of Django forms