#29276: Turn the headers of HTTPResponse from str to bytes automatically
-------------------------------------+-------------------------------------
Reporter: sanjiely | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: HTTP handling | Version: 2.0
Severity: Normal | Resolution:
Keywords: HTTPResponse header | Triage Stage:
str bytes | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Claude Paroz:
Old description:
> Python: 3.6.4
> Django: 2.0.3
>
> When I return a HttpResponse object, Django library will do the job for
> me: turn my content from str to bytes silently.
> But Django will not do the same work to the headers of HTTPResponse.
>
> That is to say, the sample as below in the official document is not
> precisely correct.
> (Page: https://docs.djangoproject.com/en/2.0/ref/request-response/)
> >>> response = HttpResponse(my_data, content_type='application/vnd.ms-
> excel')
> >>> response['Content-Disposition'] = 'attachment; filename="foo.xls"'
>
> The precise usage should be:
> >>> response['Content-Disposition'] = 'attachment;
> filename="foo.xls"'.encode('utf-8')
>
> Otherwise, everthing will out of control If I have a Chinse character in
> the filename.
> Wrong:
> >>> response['Content-Disposition'] = 'attachment; filename="中中中中中
> foo.xls"'
> Correct:
> >>> response['Content-Disposition'] = 'attachment; filename=中中中中中
> foo.xls'.encode('utf-8')
>
> Can we accept this ticket and do a minor improvement? Then we would
> happily only handle str in Django other than str and bytes together.
> Thanks.
>
> Yong Li
New description:
Python: 3.6.4
Django: 2.0.3
When I return a HttpResponse object, Django library will do the job for
me: turn my content from str to bytes silently.
But Django will not do the same work to the headers of HTTPResponse.
That is to say, the sample as below in the official document is not
precisely correct.
(Page: https://docs.djangoproject.com/en/2.0/ref/request-response/)
{{{
>>> response = HttpResponse(my_data, content_type='application/vnd.ms-
excel')
>>> response['Content-Disposition'] = 'attachment; filename="foo.xls"'
}}}
The precise usage should be:
{{{
>>> response['Content-Disposition'] = 'attachment;
filename="foo.xls"'.encode('utf-8')
}}}
Otherwise, everthing will out of control If I have a Chinse character in
the filename.
Wrong:
{{{
>>> response['Content-Disposition'] = 'attachment; filename="中中中中中
foo.xls"'
}}}
Correct:
{{{
>>> response['Content-Disposition'] = 'attachment; filename=中中中中中
foo.xls'.encode('utf-8')
}}}
Can we accept this ticket and do a minor improvement? Then we would
happily only handle str in Django other than str and bytes together.
Thanks.
Yong Li
--
--
Ticket URL: <https://code.djangoproject.com/ticket/29276#comment:3>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.fbc707547b4a1e2d801e692cfb4a6ab5%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.