在Django中设置相同名称的多个Cookie [英] Set multiple cookies with the same name in Django
问题描述
有没有办法为同一个响应中的多个域或路径设置相同名称的Cookie?以下仅设置第二个cookie:
Is there a way to set cookies with the same name for multiple domains or paths in the same response? The following only sets the second cookie:
response.set_cookie("alice", "123", domain='sub.example.com')
response.set_cookie("alice", "456", domain='example.com')
我查看了 Django实现。它使用一个字典存储cookie:
I looked at the Django implementation. It uses a dictionary for storing cookies:
self.cookies[key] = value
所以Django无法在相同的回复中设置同名的多个Cookie。
So Django cannot set multiple cookies with the same name in the same response.
是这是打算这样工作吗?您是否有办法解决这个限制?
Is this intended to work that way? Do you see a way to work around that limitation?
更新
已报告作为错误: https://code.djangoproject.com/ticket/18908
推荐答案
这可能不是问题的确切答案,但仍然。
This may not be exact answer to the question, but still.
似乎与django实现的问题,因为它只是使用cookie名称作为dict的关键。
在现实世界中,可以使用具有多个值的具有相同名称的Cookie,只要域或路径不同。我发现这个有用的 HTTP Cookie说明
This seems issue with the django implementation, as it is only using cookie name as a key in dict. In real world, one can have cookies with same name with multiple values, provided either domain or path differs. I found this useful HTTP cookies explained
更多参考:
这篇关于在Django中设置相同名称的多个Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!