summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-05-25 09:48:02 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2007-05-25 09:48:02 +0000
commit7ba850af3ba4ac7dbe3d67a2360079bce0bf0eb3 (patch)
tree9060bd793b288b686e0894d8dd33061027991da9
parent29d2094b29ccbc09f49772ca7dd773aca5114a9b (diff)
downloaddjango-7ba850af3ba4ac7dbe3d67a2360079bce0bf0eb3.tar.gz
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
-rw-r--r--django/core/handlers/modpython.py4
-rw-r--r--django/core/handlers/wsgi.py6
-rw-r--r--django/http/__init__.py19
-rw-r--r--docs/unicode.txt9
4 files changed, 30 insertions, 8 deletions
diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py
index 6370cab47c..cbb65bba5f 100644
--- a/django/core/handlers/modpython.py
+++ b/django/core/handlers/modpython.py
@@ -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):
diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
index 4320b69627..04c39834d1 100644
--- a/django/core/handlers/wsgi.py
+++ b/django/core/handlers/wsgi.py
@@ -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):
diff --git a/django/http/__init__.py b/django/http/__init__.py
index 5bc0e4ca54..481c604ba8 100644
--- a/django/http/__init__.py
+++ b/django/http/__init__.py
@@ -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
diff --git a/docs/unicode.txt b/docs/unicode.txt
index 41df214387..01fff198c1 100644
--- a/docs/unicode.txt
+++ b/docs/unicode.txt
@@ -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