summaryrefslogtreecommitdiff
path: root/django/http
diff options
context:
space:
mode:
Diffstat (limited to 'django/http')
-rw-r--r--django/http/response.py18
1 files changed, 12 insertions, 6 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):