Reputation: 1087
I run:
response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)
When the value is: aa:aa
The cookie value is: "aa:aa"
When the value is: aa
The cookie value is: aa
I need to prevent django from adding quotes when there are colons in the value
Upvotes: 9
Views: 1824
Reputation: 21
Sometimes if you need that your cookies been read from other sites, you can override the response after the set_cookie.
Python < 3.7
response.set_cookie(key, value)
response.cookies[key].coded_value = value
Python >= 3.7 Use set()
response.set_cookie(key, value)
response.cookies[key].set(key, value, value)
Upvotes: 2
Reputation: 2160
When setting the value into cookie, just encode it using urllib.parse.quote_plus()
or urllib.parse.quote()
. e.g.
value = urllib.parse.quote_plus(value)
response.set_cookie(key, value=value, expires=expires, path=path, domain=domain)
And while getting value from cookie, decode it back using urllib.parse.unquote_plus()
or urllib.parse.unquote()
respectively. e.g.
if key in request.COOKIES:
value = urllib.parse.unquote_plus(request.COOKIES[key])
Upvotes: 5