guy*_*yug 5 python string django cookies
我跑:
response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)
Run Code Online (Sandbox Code Playgroud)
当值为:aa:aa
cookie 值为:"aa:aa"
当值为:aa
cookie 值为:aa
当值中有冒号时,我需要防止 django 添加引号
将值设置到 cookie 时,只需使用urllib.parse.quote_plus()或对其进行编码即可urllib.parse.quote()。例如
value = urllib.parse.quote_plus(value)
response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)
Run Code Online (Sandbox Code Playgroud)
当从 cookie 获取值时,分别使用urllib.parse.unquote_plus()或urllib.parse.unquote()对其进行解码。例如
if key in request.COOKIES:
value = urllib.parse.unquote_plus(request.COOKIES[key])
Run Code Online (Sandbox Code Playgroud)