对Django 中request.get和request.post的区别详解

yipeiwu_com5年前Python基础

Django 中request.get和request.post的区别

POST和GET差异:

POST和GET是HTTP协议定义的与服务器交互的方法。GET一般用于获取/查询资源信息,而POST一般用于更新资源信息。另外,还有PUT和DELETE方法。

POST和GET都可以与服务器完成查,改,增,删操作。

GET提交,请求的数据会附在URL之后,以?分割URL和传输数据,多个参数用&连接;

POST提交,把提交的数据放置在HTTP包的包体中;因此,GET提交的数据会在地址栏中显示出来,而POST提交,地址栏不会改变。

GET属性

1、QueryDict类型的对象

2、包含get请求方式的所有参数

3、与url请求地址中的参数对应,位于?后面

4、参数的格式是键值对,如key1=value1

5、多个参数之间,使用&连接,如key1=value1&key2=value2

6、键是开发人员定下来的,值是可变的

示例如下

创建视图getTest1用于定义链接,getTest2用于接收一键一值,getTest3用于接收一键多值

def getTest1(request):
  return render(request,'booktest/getTest1.html')
def getTest2(request):
  return render(request,'booktest/getTest2.html')
def getTest3(request):
  return render(request,'booktest/getTest3.html')

**配置url

url(r'^getTest1/$', views.getTest1),
url(r'^getTest2/$', views.getTest2),
url(r'^getTest3/$', views.getTest3),

**创建getTest1.html,定义链接

<html>
<head>
  <title>Title</title>
</head>
<body>
链接1:一个键传递一个值
<a href="/getTest2/?a=1&b=2" rel="external nofollow" >gettest2</a><br>
链接2:一个键传递多个值
<a href="/getTest3/?a=1&a=2&b=3" rel="external nofollow" >gettest3</a>
</body>
</html>

**完善视图getTest2的代码

def getTest2(request):
  a=request.GET['a']
  b=request.GET['b']
  context={'a':a,'b':b}
  return render(request,'booktest/getTest2.html',context)

**创建getTest2.html,显示接收结果

<html>
<head>
  <title>Title</title>
</head>
<body>
a:{{ a }}<br>
b:{{ b }}
</body>
</html>

**完善视图getTest3的代码

def getTest3(request):
  a=request.GET.getlist('a')
  b=request.GET['b']
  context={'a':a,'b':b}
  return render(request,'booktest/getTest3.html',context)

**创建getTest3.html,显示接收结果

<html>
<head>
  <title>Title</title>
</head>
<body>
a:{% for item in a %}
{{ item }}
{% endfor %}
<br>
b:{{ b }}
</body>
</html>

POST属性

1、QueryDict类型的对象

2、包含post请求方式的所有参数

3、与form表单中的控件对应

4、问:表单中哪些控件会被提交?

答:控件要有name属性,则name属性的值为键,value属性的值为键,构成键值对提交。

对于checkbox控件,name属性一样为一组,当控件被选中后会被提交,存在一键多值的情况,键是开发人员定下来的,值是可变的

示例如下

定义视图postTest1

def postTest1(request):
  return render(request,'booktest/postTest1.html')

**配置url

url(r'^postTest1$',views.postTest1)

**创建模板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>

**创建视图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</title>
</head>
<body>
{{ uname }}<br>
{{ upwd }}<br>
{{ ugender }}<br>
{{ uhobby }}
</body>
</html>

以上这篇对Django 中request.get和request.post的区别详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python脚本实现分析dns日志并对受访域名排行

python脚本实现分析dns日志并对受访域名排行

前段时间有个需求是要求查一段时间的dns上的域名访问次数排行(top100),没办法,只好慢慢的去解析dns日志呗,正好学习了python,拿来练练手。 1.原始数据分析: 首先看下原始...

浅谈python socket函数中,send与sendall的区别与使用方法

在python socket编程中,有两个发送TCP的函数,send()与sendall(),区别如下: socket.send(string[, flags])  发送TCP数据,返回...

Win10下python 2.7与python 3.7双环境安装教程图解

Win10下python 2.7与python 3.7双环境安装教程图解

Win10下python 2.7与python 3.7双环境安装教程,具体内容如下所示: 1、python软件下载网址: https://www.python.org/downloads...

跨平台python异步回调机制实现和使用方法

1 将下面代码拷贝到一个文件,命名为asyncore.py 复制代码 代码如下:import socketimport selectimport sys def ds_asyncore(...

对numpy.append()里的axis的用法详解

如下所示: def append(arr, values, axis=None): """ Append values to the end of an array. Para...