Two HTML pages, stored under the Templates folder under an app.
Index.html
Submit
When you click the "Submit" button, the second page is hello.html to display the contents of the text box
The principle is to invoke the appropriate method through the action of the form
The index.html code is as follows:
<formAction= "/ok/"Method= "POST"> <inputtype= "text"name= "Q"> <Buttontype= "Submit">Submit</Button></form>
The action "/ok/" is actually called the ' OK ' method in views.py
The views.py code is as follows:
fromDjango.shortcutsImportRender_to_responsedefIndex (req):returnRender_to_response ('index.html')defOK (req): x=req. post['Q'] returnRender_to_response ('hello.html',{'Val': x})
The first index method is to transfer into the first page
The second OK method is to process the form. Through req. post[' Q '] method gets the value of the text box with the name value of ' Q ', assigns the value to the variable x,
This value is then passed to the template variable Val, which is displayed on the page.
and add the OK method to the URL map.
The code for urls.py is as follows:
Urlpatterns = [ url (r'^app1/','app1.views.index') ), URL (r'^ok/','app1.views.ok' ),
The code for the second page:
< HTML > The content you submitted is: {{val}} </ HTML >
If an error occurs, add it in the first page code
{% csrf_token %} 好像是防止攻击什么的。
Getting started with the simplest form of Django