1.bug产生背景
def _get_post(self): if not hasattr(self, '_post'): self._load_post_and_files() return self._post
def _load_post_and_files(self): # Populates self._post and self._files if self.method == 'POST': if self.environ.get('CONTENT_TYPE', '').startswith('multipart'): header_dict = dict([(k, v) for k, v in self.environ.items() if k.startswith('HTTP_')]) header_dict['Content-Type'] = self.environ.get('CONTENT_TYPE', '') self._post, self._files = http.parse_file_upload(header_dict, self.raw_post_data) else: self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict() else: self._post, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict()到这一步看不出任何问题,于是把self.raw_post_data打印了一下,发现这个还是正确的,很明显 问题就出在构造QueryDict这一步。QueryDict的构造函数如下:
def __init__(self, query_string, mutable=False, encoding=None): MultiValueDict.__init__(self) if not encoding: # *Important*: do not import settings any earlier because of note # in core.handlers.modpython. from django.conf import settings encoding = settings.DEFAULT_CHARSET self.encoding = encoding self._mutable = True for key, value in parse_qsl((query_string or ''), True): # keep_blank_values=True self.appendlist(force_unicode(key, encoding, errors='replace'), force_unicode(value, encoding, errors='replace')) self._mutable = mutable
try: from urllib.parse import parse_qsl, urlencode, quote, urljoin except ImportError: from urllib import urlencode, quote from urlparse import parse_qsl, urljoindjango1.4大同小异,只不过为了兼顾低版本Python,对最后一句再次使用try, 当从from urlparse import parse_qsl失败时,改为从 cgi 模块引入。
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0): """Parse a query given as a string argument.""" warn("cgi.parse_qsl is deprecated, use urlparse.parse_qsl instead", PendingDeprecationWarning, 2) return urlparse.parse_qsl(qs, keep_blank_values, strict_parsing)
cgi.parse_qsl(qs[, keep_blank_values[, strict_parsing]]) This function is deprecated in this module. Use urlparse.parse_qsl() instead. It is maintained here only for backward compatiblity.
def parse_qsl(qs, keep_blank_values=0, strict_parsing=0): """Parse a query given as a string argument. Arguments: qs: URL-encoded query string to be parsed keep_blank_values: flag indicating whether blank values in URL encoded queries should be treated as blank strings. A true value indicates that blanks should be retained as blank strings. The default false value indicates that blank values are to be ignored and treated as if they were not included. strict_parsing: flag indicating what to do with parsing errors. If false (the default), errors are silently ignored. If true, errors raise a ValueError exception. Returns a list, as G-d intended. """ pairs = [s2 for s1 in qs.split('&') for s2 in s1.split(';')] r = [] for name_value in pairs: if not name_value and not strict_parsing: continue nv = name_value.split('=', 1) if len(nv) != 2: if strict_parsing: raise ValueError, "bad query field: %r" % (name_value,) # Handle case of a control-name with no equal sign if keep_blank_values: nv.append('') else: continue if len(nv[1]) or keep_blank_values: name = urllib.unquote(nv[0].replace('+', ' ')) value = urllib.unquote(nv[1].replace('+', ' ')) r.append((name, value)) return r
@property def body(self): if not hasattr(self, '_body'): if self._read_started: raise Exception("You cannot access body after reading from request's data stream") try: self._body = self.read() except IOError, e: raise UnreadablePostError, e, sys.exc_traceback self._stream = StringIO(self._body) return self._body @property def raw_post_data(self): warnings.warn('HttpRequest.raw_post_data has been deprecated. Use HttpRequest.body instead.', PendingDeprecationWarning) return self.body