日常采坑,cookie不能保存中文字符串解决方案...

描述:关公不怕大刀,就怕突然脚下的坑,歪了脚...

cookie不能保存中文字符串切记!!!!!

解决方案?

方案一:

将中文字符串编码成base64,取的时候再解码,如下...

import base64
        
    #存储时 编码
      un = base64.b64encode(uname.encode('utf-8'))   # 中文不能存储在cookie中,需要编码处理
      response.set_cookie('uname', un)  # 保存用户名在cookie中

    # 获取时解码
     uname = request.COOKIES.get('uname', '')
     uname = base64.b64decode(uname).decode()  # base64解码 

方案二:

将中文字符串以字典形式保存,再将字符串序列化,最后浏览器就可取了

#
trans_uname=json.dumps(username)
response.set_cookie('username',trans_uname)
#
username=request.COOKIES.get('username')

猜你喜欢

转载自www.cnblogs.com/jum-bolg/p/12563662.html