aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/response.py
diff options
context:
space:
mode:
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):