diff options
Diffstat (limited to 'django')
| -rw-r--r-- | django/http/response.py | 18 | ||||
| -rw-r--r-- | django/template/response.py | 8 |
2 files changed, 16 insertions, 10 deletions
diff --git a/django/http/response.py b/django/http/response.py index e679c856c0..eedc03f118 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -97,8 +97,18 @@ class HttpResponseBase: status_code = 200 - def __init__(self, content_type=None, status=None, reason=None, charset=None): - self.headers = ResponseHeaders({}) + def __init__(self, content_type=None, status=None, reason=None, charset=None, headers=None): + self.headers = ResponseHeaders(headers or {}) + self._charset = charset + if content_type and 'Content-Type' in self.headers: + raise ValueError( + "'headers' must not contain 'Content-Type' when the " + "'content_type' parameter is provided." + ) + if 'Content-Type' not in self.headers: + if content_type is None: + content_type = 'text/html; charset=%s' % self.charset + self.headers['Content-Type'] = content_type self._resource_closers = [] # This parameter is set by the handler. It's necessary to preserve the # historical behavior of request_finished. @@ -114,10 +124,6 @@ class HttpResponseBase: if not 100 <= self.status_code <= 599: raise ValueError('HTTP status code must be an integer from 100 to 599.') self._reason_phrase = reason - self._charset = charset - if content_type is None: - content_type = 'text/html; charset=%s' % self.charset - self['Content-Type'] = content_type @property def reason_phrase(self): diff --git a/django/template/response.py b/django/template/response.py index 30b35b611c..9efadcd726 100644 --- a/django/template/response.py +++ b/django/template/response.py @@ -11,7 +11,7 @@ class SimpleTemplateResponse(HttpResponse): rendering_attrs = ['template_name', 'context_data', '_post_render_callbacks'] def __init__(self, template, context=None, content_type=None, status=None, - charset=None, using=None): + charset=None, using=None, headers=None): # It would seem obvious to call these next two members 'template' and # 'context', but those names are reserved as part of the test Client # API. To avoid the name collision, we use different names. @@ -33,7 +33,7 @@ class SimpleTemplateResponse(HttpResponse): # content argument doesn't make sense here because it will be replaced # with rendered template so we always pass empty string in order to # prevent errors and provide shorter signature. - super().__init__('', content_type, status, charset=charset) + super().__init__('', content_type, status, charset=charset, headers=headers) # _is_rendered tracks whether the template and context has been baked # into a final response. @@ -139,6 +139,6 @@ class TemplateResponse(SimpleTemplateResponse): rendering_attrs = SimpleTemplateResponse.rendering_attrs + ['_request'] def __init__(self, request, template, context=None, content_type=None, - status=None, charset=None, using=None): - super().__init__(template, context, content_type, status, charset, using) + status=None, charset=None, using=None, headers=None): + super().__init__(template, context, content_type, status, charset, using, headers=headers) self._request = request |
