描述:關公不怕大刀,就怕突然腳下的坑,歪了腳...
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')