如何重定向到DJANGO中包含非ascii字符的查询字符串URL?

Eri*_*ric 6 python django unicode httpresponse

如何重定向到DJANGO中包含非ascii字符的查询字符串URL?

当我使用包含字符的return HttpResponseRedirect(u'/page/?title=' + query_string)地方时,我收到错误query_string??

'ascii'编解码器不能编码21-26位的字符:序号不在范围内(128),HTTP响应头必须是US-ASCII格式...

Ale*_*lli 6

HttpResponseRedirect(((u'/page/?title=' + query_string).encode('utf-8'))
Run Code Online (Sandbox Code Playgroud)

是第一个尝试(因为UTF8是唯一可以处理所有Unicode字符的流行编码).这肯定应该摆脱你正在观察的异常 - 然后问题转向确保处理程序/page可以正确处理UTF-8编码的查询(可能通过将它们解码回Unicode).但是,严格来说,这部分并不是你要问的这个具体问题的密切关系!


sla*_*nic 6

django方式:

from django.http import HttpResponseRedirect
from django.utils.http import urlquote

return HttpResponseRedirect(u'/page/?title=%s' % urlquote(query_string))
Run Code Online (Sandbox Code Playgroud)