cookie设置中文问题

set_cookie:cookie值为中文时会出错。

错误:
Traceback (most recent call last):
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "/usr/lib/python3.5/wsgiref/handlers.py", line 333, in send_headers
    self._write(bytes(self.headers))
  File "/usr/lib/python3.5/wsgiref/headers.py", line 142, in __bytes__
    return str(self).encode('iso-8859-1')
UnicodeEncodeError: 'latin-1' codec can't encode character '\u90d1' in position 288: ordinal not in range(256)

原因:中文不能通过 'latin-1'编码

解决:

设置cookie:
newuser = username.encode('utf-8').decode('latin-1')
response.set_cookie('uname',newuser)

获取cookie:
if request.COOKIES.get('uname'):
    context['uname'] = request.COOKIES['uname'].encode('latin-1').decode('utf-8')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值