Opened 10 years ago

Last modified 10 years ago

#24598 closed Bug

JsonResponse loses encoding support in Content-Type header — at Initial Version

Reported by: Curtis Maloney Owned by: nobody
Component: HTTP handling Version: 1.8
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: yes
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

By default when a content_type is not passed to HttpResponse it will create one from settings.DEFAULT_CONTENT_TYPE, and attach the encoding.

So, when unspecified, the header will appear as:

Content-Type: text/html; encoding=utf-8

However, if you specify the content_type, this action is no taken.

That's understandable in a "consenting adults" context, however the JsonResponse sets content_type using kwargs.setdefault, thus forcing the loss of encoding annotation in all responses.

I propose instead that HttpResponseBase check for a default_content_type property to override settings.DEFAULT_CONTENT_TYPE, thus:

diff --git a/django/http/response.py b/django/http/response.py
index c8d6930..40186b5 100644
--- a/django/http/response.py
+++ b/django/http/response.py
@@ -54,8 +54,8 @@ class HttpResponseBase(six.Iterator):
         self._reason_phrase = reason
         self._charset = charset
         if content_type is None:
-            content_type = '%s; charset=%s' % (settings.DEFAULT_CONTENT_TYPE,
-                                               self.charset)
+            content_type = getattr(self, 'default_content_type', settings.DEFAULT_CONTENT_TYPE)
+            content_type = '%s; charset=%s' % (content_type, self.charset)
         self['Content-Type'] = content_type

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top