diff --git a/django/http/request.py b/django/http/request.py index d1a435e95c4..f7f89913ef6 100644 --- a/django/http/request.py +++ b/django/http/request.py @@ -14,7 +14,7 @@ from django.core.files import uploadhandler from django.http.multipartparser import MultiPartParser, MultiPartParserError from django.utils.datastructures import ImmutableList, MultiValueDict from django.utils.deprecation import RemovedInDjango30Warning -from django.utils.encoding import escape_uri_path, force_bytes, iri_to_uri +from django.utils.encoding import escape_uri_path, iri_to_uri from django.utils.functional import cached_property from django.utils.http import is_same_domain, limited_parse_qsl @@ -511,7 +511,7 @@ class QueryDict(MultiValueDict): """ output = [] if safe: - safe = force_bytes(safe, self.encoding) + safe = safe.encode(self.encoding) def encode(k, v): return '%s=%s' % ((quote(k, safe), quote(v, safe))) @@ -519,9 +519,10 @@ class QueryDict(MultiValueDict): def encode(k, v): return urlencode({k: v}) for k, list_ in self.lists(): - k = force_bytes(k, self.encoding) - output.extend(encode(k, force_bytes(v, self.encoding)) - for v in list_) + output.extend( + encode(k.encode(self.encoding), v.encode(self.encoding)) + for v in list_ + ) return '&'.join(output) diff --git a/django/http/response.py b/django/http/response.py index b6f29cc0563..b21b73f2470 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -13,7 +13,7 @@ from django.core.exceptions import DisallowedRedirect from django.core.serializers.json import DjangoJSONEncoder from django.http.cookie import SimpleCookie from django.utils import timezone -from django.utils.encoding import force_bytes, iri_to_uri +from django.utils.encoding import iri_to_uri from django.utils.http import http_date _charset_from_content_type_re = re.compile(r';\s*charset=(?P[^\s;]+)', re.I) @@ -228,9 +228,8 @@ class HttpResponseBase: return bytes(value) if isinstance(value, str): return bytes(value.encode(self.charset)) - - # Handle non-string types (#16494) - return force_bytes(value, self.charset) + # Handle non-string types. + return str(value).encode(self.charset) # These methods partially implement the file-like object interface. # See https://docs.python.org/3/library/io.html#io.IOBase