aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/response.py
diff options
context:
space:
mode:
authorTom Christie2013-06-05 13:33:19 +0100
committerTom Christie2013-06-05 13:33:19 +0100
commitde00ec95c3007dd90b5b01f7486b430699ea63c1 (patch)
treed2ce8037d446fd9133b3d6a77ebcc49350d7ebc3 /rest_framework/response.py
parent9428d6ddb5ebc2d5d9c8557a52be09f0def69cca (diff)
parent2ca243a1144bb2a5461767a21ed14dec1d2b8dc2 (diff)
downloaddjango-rest-framework-de00ec95c3007dd90b5b01f7486b430699ea63c1.tar.bz2
Merge master
Diffstat (limited to 'rest_framework/response.py')
-rw-r--r--rest_framework/response.py24
1 files changed, 19 insertions, 5 deletions
diff --git a/rest_framework/response.py b/rest_framework/response.py
index 26e4ab37..5877c8a3 100644
--- a/rest_framework/response.py
+++ b/rest_framework/response.py
@@ -1,5 +1,5 @@
"""
-The Response class in REST framework is similiar to HTTPResponse, except that
+The Response class in REST framework is similar to HTTPResponse, except that
it is initialized with unrendered data, instead of a pre-rendered string.
The appropriate renderer is called during Django's template response rendering.
@@ -12,13 +12,13 @@ from rest_framework.compat import six
class Response(SimpleTemplateResponse):
"""
- An HttpResponse that allows it's data to be rendered into
+ An HttpResponse that allows its data to be rendered into
arbitrary media types.
"""
def __init__(self, data=None, status=200,
template_name=None, headers=None,
- exception=False):
+ exception=False, content_type=None):
"""
Alters the init arguments slightly.
For example, drop 'template_name', and instead use 'data'.
@@ -30,6 +30,7 @@ class Response(SimpleTemplateResponse):
self.data = data
self.template_name = template_name
self.exception = exception
+ self.content_type = content_type
if headers:
for name, value in six.iteritems(headers):
@@ -46,8 +47,21 @@ class Response(SimpleTemplateResponse):
assert context, ".renderer_context not set on Response"
context['response'] = self
- self['Content-Type'] = media_type
- return renderer.render(self.data, media_type, context)
+ charset = renderer.charset
+ content_type = self.content_type
+
+ if content_type is None and charset is not None:
+ content_type = "{0}; charset={1}".format(media_type, charset)
+ elif content_type is None:
+ content_type = media_type
+ self['Content-Type'] = content_type
+
+ ret = renderer.render(self.data, media_type, context)
+ if isinstance(ret, six.text_type):
+ assert charset, 'renderer returned unicode, and did not specify ' \
+ 'a charset value.'
+ return bytes(ret.encode(charset))
+ return ret
@property
def status_text(self):