在url中传参遇到中文时,需要将参数编码后传递,(我的开发环境是py2)
import urllib
class ShiTu(APIView):
def post(request):
ip='xxx'
port='xxxx'
key=u'我是测试数据'
url = 'http://{ip}:{port}/resource/get_resource/?key={key}'.format(ip=in_ip,port=port,key=urllib.quote(key.encode('utf-8')))
return redirect(url)
而接收方(重定向去到的那端)直接获取key这个参数就是unicode格式
key = request.GET.get('key')
# type(key) # unicode
参考https://blog.csdn.net/shanliangliuxing/article/details/8112986
这篇文章中还提到另一种方法
from django.utils.http import urlquote
#return的时候在URL前加上urlquote
return HttpResponseRedirect(urlquote("/wiki/%s" % page.pagename))
但是测试发现这样会将整个url全部编码,而接收方(重定向去到的那端)无法得到请求,所以用了上面所述的方法只将中文参数进行编码来发送请求,然后成功得到了响应。
在ipython中测试编解码