UnicodeEncodeError on redirect to non-ASCII Location
|Reported by:||Ilya Semenov||Owned by:||nobody|
|Cc:||yoanblanc, oldium.pro@…||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
If a view for a URL which contains unicode characters returns a HttpResponseRedirect to a non-absolute URL, django crashes.
def myview(request): print request.path # '/info/Интеграция_CMS/' return HttpResponseRedirect('?edit=1')
gives the following error message:
URI: '/info/\xd0\x98\xd0\xbd\xd1\x82\xd0\xb5\xd0\xb3\xd1\x80\xd0\xb0\xd1\x86\xd0\xb8\xd1\x8f_CMS/' ... Traceback (most recent call last): ... File "/usr/lib/python2.5/site-packages/django/http/utils.py", line 20, in fix_location_header response['Location'] = request.build_absolute_uri(response['Location']) File "/usr/lib/python2.5/site-packages/django/http/__init__.py", line 314, in __setitem__ header, value = self._convert_to_ascii(header, value) File "/usr/lib/python2.5/site-packages/django/http/__init__.py", line 306, in _convert_to_ascii yield value.encode('us-ascii') UnicodeEncodeError: 'ascii' codec can't encode characters in position 28-37: ordinal not in range(128), HTTP response headers must be in US-ASCII format
The cause of the problem is that #10267 was incorrectly fixed by , leaving many other cases open. The call to iri_to_uri should be moved from HttpResponseRedirect?.init (and other places where it had been copy-pasted) deeper to django.utils.http.fix_location_header.
Apart of my problem, it was reported in #10267 that django.views.generic.simple.redirect_to and django.contrib.redirects also suffer from the similar encoding issue, which would also be resolved once fix_location_header is fixed appropriately.
For those Google strangers who are interested in a workaround, you can do as follows:
def myview(request): print request.path # '/info/Интеграция_CMS/' return HttpResponseRedirect('?edit=1') # crashes return HttpResponseRedirect(request.path + '?edit=1') # doesn't crash
Change History (29)
comment:16 Changed 7 years ago by
|Summary:||Crash on redirect to a relative URL if request.path is unicode → UnicodeEncodeError on redirect to non-ASCII Location|