Django视图-Get、Post属性
Get属性
请求格式:在请求地址结尾使用?,之后以"键=值"的格式拼接,多个键值对之间以&连接。
例:网址如下
http://www.baidu.cn/?a=10&b=20&c=python
其中的请求参数为:
a=10&b=20&c=python
(1) 分析请求参数,键为'a'、'b'、'c',值为'10'、'20'、'python'。
(2) 在Django中可以使用HttpRequest对象的GET属性获得get方式请求的参数。
(3) GET属性是一个QueryDict类型的对象,键和值都是字符串类型。
(4) 键是开发人员在编写代码时确定下来的。
(5) 值是根据数据生成的。
POST属性
使用form表单请求时,method方式为post则会发起post方式的请求,需要使用HttpRequest对象的POST属性接收参数,POST属性是一个QueryDict类型的对象。
问:表单form如何提交参数呢?
答:表单控件name属性的值作为键,value属性的值为值,构成键值对提交。
如果表单控件没有name属性则不提交。
对于checkbox控件,name属性的值相同为一组,被选中的项会被提交,出现一键多值的情况。
键是表单控件name属性的值,是由开发人员编写的。
值是用户填写或选择的。
定义视图postTest1
def postTest1(request):
return render(request,'booktest/postTest1.html')
配置url
url(r'^postTest1$',views.postTest1)
创建模板postTest1.html

创建视图postTest2接收请求的数据
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
url(r'^postTest2$',views.postTest2)
创建模板postTest2.html
<html>
<head>
<title>标题</title>
</head>
<body>
{{ uname }}<br>
{{ upwd }}<br>
{{ ugender }}<br>
{{ uhobby }}</body>
</html>