解决Django的request.POST获取不到内容的问题


Posted in Python onMay 28, 2018

我通过如下的一段程序发送post请求:

import urllib3
pool = urllib3.connection_from_url('http://127.0.0.1:8090')
resp = pool.request('POST', '/polls/', fields={'key1':'value1', 'key2':'value2'}, headers={'Content-Type':'application/json'}, encode_multipart=False)

在服务器端我用request.POST期望能获取到<QueryDict: {u'key2': [u'value2'], u'key1': [u'value1']}>,但是我发现获取到的是一个空的<QueryDict: {}>,用reqyest.body是能获取到原始的请求内容key2=value2&key1=value1的。

这个时候只能去文档中找答案了,但是貌似Django中的文档也没给出我答案,这时候我就只能通过源码来找答案了,下面是class HttpRequest(object)中获取POST QueryDict的函数部分:

def _load_post_and_files(self):
  """Populate self._post and self._files if the content-type is a form type"""
  if self.method != 'POST':
   self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()
   return
  if self._read_started and not hasattr(self, '_body'):
   self._mark_post_parse_error()
   return

  if self.content_type == 'multipart/form-data':
   if hasattr(self, '_body'):
    # Use already read data
    data = BytesIO(self._body)
   else:
    data = self
   try:
    self._post, self._files = self.parse_file_upload(self.META, data)
   except MultiPartParserError:
    # An error occurred while parsing POST data. Since when
    # formatting the error the request handler might access
    # self.POST, set self._post and self._file to prevent
    # attempts to parse POST data again.
    # Mark that an error occurred. This allows self.__repr__ to
    # be explicit about it instead of simply representing an
    # empty POST
    self._mark_post_parse_error()
    raise
  elif self.content_type == 'application/x-www-form-urlencoded':
   self._post, self._files = QueryDict(self.body, encoding=self._encoding), MultiValueDict()
  else:
   self._post, self._files = QueryDict(encoding=self._encoding), MultiValueDict()

函数看起来有点长,但是我们只要关注后面的if elif else这三个分支即可,从elif self.content_type == 'application/x-www-form-urlencoded':这个分支能看到只有请求header中的'Content-Type':'application/x-www-form-urlencoded'才会填充request.POST,其它情况下只有一个空的<QueryDict: {}>。

从这个问题也看到了Django对'Content-Type':'application/json'没有做任何处理,跟我预想的有一点不一样。

以上这篇解决Django的request.POST获取不到内容的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python3.3使用tkinter开发猜数字游戏示例
Mar 14 Python
Python处理JSON数据并生成条形图
Aug 05 Python
python编程实现希尔排序
Apr 13 Python
Python封装原理与实现方法详解
Aug 28 Python
使用python Fabric动态修改远程机器hosts的方法
Oct 26 Python
python调用webservice接口的实现
Jul 12 Python
np.dot()函数的用法详解
Jan 17 Python
Python continue语句实例用法
Feb 06 Python
Python切割图片成九宫格的示例代码
Mar 10 Python
python 如何把docker-compose.yaml导入到数据库相关条目里
Jan 15 Python
教你怎么用Python监控愉客行车程
Apr 29 Python
python字符串的一些常见实用操作
Apr 06 Python
Django后台获取前端post上传的文件方法
May 28 #Python
基于Django URL传参 FORM表单传数据 get post的用法实例
May 28 #Python
浅析Python装饰器以及装饰器模式
May 28 #Python
Python装饰器知识点补充
May 28 #Python
更换Django默认的模板引擎为jinja2的实现方法
May 28 #Python
django manage.py扩展自定义命令方法
May 27 #Python
python实现windows下文件备份脚本
May 27 #Python
You might like
php页面跳转代码 输入网址跳转到你定义的页面
2013/03/28 PHP
php简单日历函数
2015/10/28 PHP
jquery拖动插件(jquery.drag)使用介绍
2013/06/18 Javascript
Windows 系统下安装和部署Egret的开发环境
2014/07/31 Javascript
基于jQuery Bar Indicator 插件实现进度条展示效果
2015/09/30 Javascript
AngularJS Module方法详解
2015/12/08 Javascript
jQuery中inArray方法注意事项分析
2016/01/25 Javascript
手机图片预览插件photoswipe.js使用总结
2016/08/25 Javascript
基于Vue实现timepicker
2017/04/25 Javascript
jQuery实现的简单前端搜索功能示例
2017/10/28 jQuery
Angular实现的内置过滤器orderBy排序与模糊查询功能示例
2017/12/29 Javascript
python实现socket端口重定向示例
2014/02/10 Python
零基础写python爬虫之抓取糗事百科代码分享
2014/11/06 Python
初步理解Python进程的信号通讯
2015/04/09 Python
利用Python如何生成hash值示例详解
2017/12/20 Python
python实现Adapter模式实例代码
2018/02/09 Python
浅谈pycharm出现卡顿的解决方法
2018/12/03 Python
python利用re,bs4,requests模块获取股票数据
2019/07/29 Python
python实现BP神经网络回归预测模型
2019/08/09 Python
Python3 全自动更新已安装的模块实现
2020/01/06 Python
vue学习笔记之动态组件和v-once指令简单示例
2020/02/29 Python
使用python+poco+夜神模拟器进行自动化测试实例
2020/04/23 Python
python异步Web框架sanic的实现
2020/04/27 Python
python 引用传递和值传递详解(实参,形参)
2020/06/05 Python
浅谈python锁与死锁问题
2020/08/14 Python
英国领先的电动可调床制造商:Laybrook
2019/12/26 全球购物
2014的自我评价
2014/01/13 职场文书
乡镇庆八一活动方案
2014/02/02 职场文书
大学生交通专业求职信
2014/09/01 职场文书
国际残疾人日广播稿范文
2014/10/09 职场文书
2015年基层党组织公开承诺书
2015/01/21 职场文书
党员承诺书格式范文
2015/04/28 职场文书
详解Redis复制原理
2021/06/04 Redis
Mysql8.0递归查询的简单用法示例
2021/08/04 MySQL
mysql配置SSL证书登录的实现
2021/09/04 MySQL
JavaScript中reduce()的用法
2022/05/11 Javascript