diff options
| author | SaJH <wogur981208@gmail.com> | 2024-09-11 21:23:23 +0900 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2024-10-16 11:52:22 +0200 |
| commit | 4a685bc0dca5298a7d5a4e162120a90cac7fd1a4 (patch) | |
| tree | 4ecc8f20d0fc48c0a95e13e02a91b7eec8a20fb0 /django | |
| parent | ec7d69035a408b357f1803ca05a7c991cc358cfa (diff) | |
Fixed #35727 -- Added HttpResponse.text property.
Signed-off-by: SaJH <wogur981208@gmail.com>
Diffstat (limited to 'django')
| -rw-r--r-- | django/http/response.py | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/django/http/response.py b/django/http/response.py index abe71718f2..1dbaf46add 100644 --- a/django/http/response.py +++ b/django/http/response.py @@ -21,6 +21,7 @@ from django.http.cookie import SimpleCookie from django.utils import timezone from django.utils.datastructures import CaseInsensitiveMapping from django.utils.encoding import iri_to_uri +from django.utils.functional import cached_property from django.utils.http import content_disposition_header, http_date from django.utils.regex_helper import _lazy_re_compile @@ -408,6 +409,11 @@ class HttpResponse(HttpResponseBase): content = self.make_bytes(value) # Create a list of properly encoded bytestrings to support write(). self._container = [content] + self.__dict__.pop("text", None) + + @cached_property + def text(self): + return self.content.decode(self.charset or "utf-8") def __iter__(self): return iter(self._container) @@ -461,6 +467,12 @@ class StreamingHttpResponse(HttpResponseBase): ) @property + def text(self): + raise AttributeError( + "This %s instance has no `text` attribute." % self.__class__.__name__ + ) + + @property def streaming_content(self): if self.is_async: # pull to lexical scope to capture fixed reference in case |
