mirror of
https://github.com/django/django.git
synced 2025-07-03 17:29:12 +00:00
unicode: Added a more convenient way to set/change the encoding on a request
instance. git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5342 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
29d2094b29
commit
7ba850af3b
@ -49,7 +49,7 @@ class ModPythonRequest(http.HttpRequest):
|
||||
if 'content-type' in self._req.headers_in and self._req.headers_in['content-type'].startswith('multipart'):
|
||||
self._post, self._files = http.parse_file_upload(self._req.headers_in, self.raw_post_data)
|
||||
else:
|
||||
self._post, self._files = http.QueryDict(self.raw_post_data), datastructures.MultiValueDict()
|
||||
self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
|
||||
|
||||
def _get_request(self):
|
||||
if not hasattr(self, '_request'):
|
||||
@ -58,7 +58,7 @@ class ModPythonRequest(http.HttpRequest):
|
||||
|
||||
def _get_get(self):
|
||||
if not hasattr(self, '_get'):
|
||||
self._get = http.QueryDict(self._req.args)
|
||||
self._get = http.QueryDict(self._req.args, encoding=self._encoding)
|
||||
return self._get
|
||||
|
||||
def _set_get(self, get):
|
||||
|
@ -113,9 +113,9 @@ class WSGIRequest(http.HttpRequest):
|
||||
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), datastructures.MultiValueDict()
|
||||
self._post, self._files = http.QueryDict(self.raw_post_data, encoding=self._encoding), datastructures.MultiValueDict()
|
||||
else:
|
||||
self._post, self._files = http.QueryDict(''), datastructures.MultiValueDict()
|
||||
self._post, self._files = http.QueryDict('', encoding=self._encoding), datastructures.MultiValueDict()
|
||||
|
||||
def _get_request(self):
|
||||
if not hasattr(self, '_request'):
|
||||
@ -125,7 +125,7 @@ class WSGIRequest(http.HttpRequest):
|
||||
def _get_get(self):
|
||||
if not hasattr(self, '_get'):
|
||||
# The WSGI spec says 'QUERY_STRING' may be absent.
|
||||
self._get = http.QueryDict(self.environ.get('QUERY_STRING', ''))
|
||||
self._get = http.QueryDict(self.environ.get('QUERY_STRING', ''), encoding=self._encoding)
|
||||
return self._get
|
||||
|
||||
def _set_get(self, get):
|
||||
|
@ -18,6 +18,10 @@ class Http404(Exception):
|
||||
|
||||
class HttpRequest(object):
|
||||
"A basic HTTP request"
|
||||
|
||||
# The encoding used in GET/POST dicts. None means use default setting.
|
||||
_encoding = None
|
||||
|
||||
def __init__(self):
|
||||
self.GET, self.POST, self.COOKIES, self.META, self.FILES = {}, {}, {}, {}, {}
|
||||
self.path = ''
|
||||
@ -43,6 +47,21 @@ class HttpRequest(object):
|
||||
def is_secure(self):
|
||||
return os.environ.get("HTTPS") == "on"
|
||||
|
||||
def _set_encoding(self, val):
|
||||
"""
|
||||
Sets the encoding used for GET/POST accesses.
|
||||
"""
|
||||
self._encoding = val
|
||||
if hasattr(self, '_get'):
|
||||
self.GET.encoding = val
|
||||
if hasattr(self, '_post'):
|
||||
self.POST.encoding = val
|
||||
|
||||
def _get_encoding(self):
|
||||
return self._encoding
|
||||
|
||||
encoding = property(_get_encoding, _set_encoding)
|
||||
|
||||
def parse_file_upload(header_dict, post_data):
|
||||
"Returns a tuple of (POST MultiValueDict, FILES MultiValueDict)"
|
||||
import email, email.Message
|
||||
|
@ -337,14 +337,17 @@ be returned exactly as they were submitted by the client.
|
||||
By default, the ``DEFAULT_CHARSET`` setting is used as the assumed encoding
|
||||
for form data. If you need to change this for a particular form, you can set
|
||||
the ``encoding`` attribute on the ``GET`` and ``POST`` data structures. For
|
||||
example::
|
||||
convenience, changing the ``encoding`` property on an ``HttpRequest`` instance
|
||||
does this for you. For example::
|
||||
|
||||
def some_view(request):
|
||||
# We know that the data must be encoded as KOI8-R (for some reason).
|
||||
request.GET.encoding = 'koi8-r'
|
||||
request.POST.encoding = 'koi8-r'
|
||||
request.encoding = 'koi8-r'
|
||||
...
|
||||
|
||||
You can even change the encoding after having accessed ``request.GET`` or
|
||||
``request.POST`` and all subsequent accesses will use the new encoding.
|
||||
|
||||
It will typically be very rare that you would need to worry about changing the
|
||||
form encoding. However, if you are talking to a legacy system or a system
|
||||
beyond your control with particular ideas about encoding, you do have a way to
|
||||
|
Loading…
x
Reference in New Issue
Block a user