Django接受前端数据的几种方法总结
作者:jingxian 时间:2021-11-26 23:32:53
背景
测试工具箱写到一半,今天遇到了一个前后端数据交互的问题,就一起做一下整理。
环境
--------------------------------------------------------
版本相关
操作系统:Mac OS X EI Caption
Python版本:2.7
IDE:PyCharm
Django: 1.8.2
---------------------------------------------------------
注: 我测试的是Get方法,POST方法也同样适用
字符型
字符型的数据相对好获取,前端传递的方法如下:
sendData = { "exporttype": exporttype,
"bugids": bugids,
"test": JSON.stringify({"test": "test"})
};
在Django的后端只要使用exporttype = request.GET.get("exporttype")
就能正常的获取到这个数据了。
注意: 在Python2.7中数据是unicode编码的,如果要使用,有时候需要进行转str
结果示例:
Excle <type 'unicode'>
数组型
获取数组型的数据如果使用获取字符串的数据的方法,打出的结果是None。我们要使用这个方法:
bugids = request.GET.getlist("bugids[]")
这样获取的数据就是数组类型。
注意: 获取的数组中的元素是unicode编码的,在某些时候使用需要转编码
结果示例:
•传递的url
[14/Jul/2016 11:00:41]"GET /testtools/exportbug/?exporttype=Excle&bugids%5B%5D=102&bugids%5B%5D=101&bugids%5B%5D
•获取的数据
[u'102', u'101', u'100', u'99', u'98', u'97', u'96', u'95', u'94', u'93', u'92', u'91', u'90', u'89', u'88', u'87'
字典型
字典型数据其实可以当成字符串数据来处理,获取到对应字符串后使用JSON模块做一下格式化就行了。
对于前端来说,传递字典型的数据就是传递JSON数据,所以使用的方法是:
"test": JSON.stringify({"test": "test"})
结果示例:
{"test":"test"} <type 'unicode'>
相关源码
•Get方法
Get方法是wsgi里面的一个方法。
def GET(self):
# The WSGI spec says 'QUERY_STRING' may be absent.
raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '')
return http.QueryDict(raw_query_string, encoding=self._encoding)
最终返回的是一个http.QueryDict(raw_query_string, encoding=self._encoding)http的原始数据,而QueryDict继承于MultiValueDict ,所以我们直接看MultiValueDict就好了。
•MultiValueDict
其实源码看起来并不难。
def get(self, key, default=None):
"""
Returns the last data value for the passed key. If key doesn't exist
or value is an empty list, then default is returned.
"""
try:
val = self[key]
except KeyError:
return default
if val == []:
return default
return val
def getlist(self, key, default=None):
"""
Returns the list of values for the passed key. If key doesn't exist,
then a default value is returned.
"""
try:
return super(MultiValueDict, self).__getitem__(key)
except KeyError:
if default is None:
return []
return default
def __getitem__(self, key):
"""
Returns the last data value for this key, or [] if it's an empty list;
raises KeyError if not found.
"""
try:
list_ = super(MultiValueDict, self).__getitem__(key)
except KeyError:
raise MultiValueDictKeyError(repr(key))
try:
return list_[-1]
except IndexError:
return []
getlist方法也就是把数据全部整合一下,返回回来。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
text-overflow实现标题省略号功能
pygame游戏之旅 按钮上添加文字的方法
![](https://img.aspxhome.com/file/2023/3/87753_0s.jpg)
python给视频添加背景音乐并改变音量的具体方法
php函数之strtr和str_replace的用法详解以及效率分析
TensorFlow实现创建分类器
![](https://img.aspxhome.com/file/2023/4/104914_0s.jpg)
python实现在pandas.DataFrame添加一行
Python语言生成水仙花数代码示例
![](https://img.aspxhome.com/file/2023/9/110439_0s.png)
JS获取checkbox的个数简单实例
django实现支付宝支付实例讲解
![](https://img.aspxhome.com/file/2023/1/62551_0s.png)
10个提高网站可用性的实用技巧[译]
![](https://img.aspxhome.com/file/UploadPic/20096/12/e110ff69d4158da954232063c6e0f6d7-48s.jpeg)
通过实例学习Python Excel操作
![](https://img.aspxhome.com/file/2023/2/78782_0s.png)
火狐浏览器:浏览数据新方式(附模拟图)[译]
![](https://img.aspxhome.com/file/UploadPic/20094/23/faaborg-49s.jpg)
selenium自动化测试简单准备
![](https://img.aspxhome.com/file/2023/4/75904_0s.png)
python中Pexpect的工作流程实例讲解
golang中使用匿名结构体的方法
PHP 巧用数组降低程序的时间复杂度
详解Python中的数据清洗工具flashtext
![](https://img.aspxhome.com/file/2023/5/69645_0s.png)
Python实现栈和队列的简单操作方法示例
![](https://img.aspxhome.com/file/2023/0/111870_0s.png)
Python pyecharts实时画图自定义可视化经纬度热力图
![](https://img.aspxhome.com/file/2023/8/110478_0s.png)
css reset中的list-style:none
![](https://img.aspxhome.com/file/UploadPic/20105/26/01-62s.jpg)