最近在做的一个项目需要python做服务端,我一个数据分析的来写后端了,哈哈,这是要发展为全栈了吗。那就全栈吧,~~
首先呢,我是这样写的:
Django端接收:
def cipin_statistics(request):
result = request.POST
print(result)
return HttpResponse('{"status":"'+status+'","code":"2","result":[' + str(result) + ']}')
脚本请求
paramsquery = {
"data":{
"file":"D:/python_project/project_hesc/myapp/test.txt",
"text":""
},
"rule":{
"file":"D:/python_project/project_hesc/myapp/adress.txt",
"text":""
}
}
r = requests.request('POST','http://192.168.71.231:8000/hesc/pro/',data=paramsquery)
print(r.text)
但是得到的结果是这样的:
Django version 2.1.7, using settings 'Django_project.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.
<QueryDict: {'data': ['file', 'text'], 'rule': ['file', 'text']}>
[20/Mar/2019 18:00:05] "POST /hesc/pro/ HTTP/1.1" 200 12
这显然要骂街的吗,所以我就有了以下的修改:
Django后端:
def cipin_statistics(request):
response_data = json.loads(request.body)
data_file = response_data['data']['file']
rule_file = response_data['rule']['file']
data_text = response_data['data']['text']
rule_text = response_data['rule']['text']
print(data_file,data_text)
print(rule_file,rule_text)
HttpResponse('{"status":"'+status+'","code":"2","result":[' + str(result) + ']}')
脚本端:
paramsquery = {
"data":{
"file":"D:/python_project/project_hesc/myapp/test.txt",
"text":""
},
"rule":{
"file":"D:/python_project/project_hesc/myapp/adress.txt",
"text":""
}
}
r = requests.request('POST','http://192.168.71.231:8000/hesc/pro/',json=paramsquery)
print(r.text)
这样的话在Django端打印出来的是:
Django version 2.1.7, using settings 'project_hesc.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CTRL-BREAK.
D:/python_project/project_hesc/myapp/test.txt
D:/python_project/project_hesc/myapp/adress.txt
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\lenovo\AppData\Local\Temp\jieba.cache
Loading model cost 0.590 seconds.
Prefix dict has been built succesfully.
[20/Mar/2019 18:05:15] "POST /hesc/pro/ HTTP/1.1" 200 693
这样就完事了吗,那两个文件路径就是打印出来的结果。。,看出区别在哪了吗?嘻嘻。。
本文介绍了在Django后端处理POST请求时遇到嵌套参数无法正常接收的问题,通过示例代码展示了初始错误情况及解决方法,最终成功获取到请求中的文件路径。
598

被折叠的 条评论
为什么被折叠?



