POST屬性
- QueryDict類型的對象
- 包含post請求方式的所有參數(shù)
- 與form表單中的控件對應(yīng)
- 問:表單中哪些控件會被提交博脑?
- 答:控件要有name屬性驻谆,則name屬性的值為鍵,value屬性的值為鍵奈嘿,構(gòu)成鍵值對提交
- 對于checkbox控件貌虾,name屬性一樣為一組,當(dāng)控件被選中后會被提交裙犹,存在一* 鍵多值的情況
- 鍵是開發(fā)人員定下來的尽狠,值是可變的
- 示例如下
定義視圖postTest1
def postTest1(request):
return render(request,'booktest/postTest1.html')
url(r'^postTest1$',views.postTest1)
- 創(chuàng)建模板postTest1.html
<html>
<head>
<title>Title</title>
</head>
<body>
<form method="post" action="/postTest2/">
姓名:<input type="text" name="uname"/><br>
密碼:<input type="password" name="upwd"/><br>
性別:<input type="radio" name="ugender" value="1"/>男
<input type="radio" name="ugender" value="0"/>女<br>
愛好:<input type="checkbox" name="uhobby" value="胸口碎大石"/>胸口碎大石
<input type="checkbox" name="uhobby" value="跳樓"/>跳樓
<input type="checkbox" name="uhobby" value="喝酒"/>喝酒
<input type="checkbox" name="uhobby" value="爬山"/>爬山<br>
<input type="submit" value="提交"/>
</form>
</body>
</html>
- 創(chuàng)建視圖postTest2接收請求的數(shù)據(jù)
def postTest2(request):
uname=request.POST['uname']
upwd=request.POST['upwd']
ugender=request.POST['ugender']
uhobby=request.POST.getlist('uhobby')
context={'uname':uname,'upwd':upwd,'ugender':ugender,'uhobby':uhobby}
return render(request,'booktest/postTest2.html',context)
url(r'^postTest2$',views.postTest2)
- 創(chuàng)建模板postTest2.html
<html>
<head>
<title>Title</title>
</head>
<body>
{{ uname }}<br>
{{ upwd }}<br>
{{ ugender }}<br>
{{ uhobby }}
</body>
</html>
- 注意:使用表單提交,注釋掉settings.py中的中間件crsf
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者