summaryrefslogtreecommitdiff
path: root/django/http/response.py
diff options
context:
space:
mode:
authorTom Carrick <tom@carrick.eu>2020-09-15 12:43:37 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2020-10-07 09:19:57 +0200
commitdcb69043d0de45bb55998fc418d93c28bc7689ae (patch)
treeac58b959d8b749965841721e6cc2a58784bb51c3 /django/http/response.py
parent2e7cc95499f758a1c4aa036cbf1dcddf82a89ea2 (diff)
Fixed #32002 -- Added headers parameter to HttpResponse and subclasses.
Diffstat (limited to 'django/http/response.py')
-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):