From 95b7d01d3856da323a610338aaa6882e82e4cc48 Mon Sep 17 00:00:00 2001 From: Keryn Knight Date: Wed, 23 Feb 2022 16:30:19 +0000 Subject: [PATCH] Refs #33546 -- Optimized handling content types in HttpResponseBase.__init__(). This removes an extraneous conditional causing "Content-Type" to be checked within the ResponseHeaders twice, if a content_type parameter is provided. --- django/http/response.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/django/http/response.py b/django/http/response.py index ce49d78d9bf..8ae4719575b 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -111,15 +111,15 @@ class HttpResponseBase: ): self.headers = ResponseHeaders(headers) self._charset = charset - if content_type and "Content-Type" in self.headers: + if "Content-Type" not in self.headers: + if content_type is None: + content_type = f"text/html; charset={self.charset}" + self.headers["Content-Type"] = content_type + elif content_type: 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.