[英]Django doesn't set cookie and doesn't create session
I have a problem setting cookies in Django.我在Django中设置cookies有问题。
Basically I have 3 different cookie I wanna set:基本上我要设置 3 个不同的 cookie:
For some reason Access and Refresh tokens are set, but the Session ID (SSID) doesn't set.由于某种原因,设置了访问和刷新令牌,但未设置 Session ID (SSID)。 If I change key of "SSID" to for example "TEST_COOKIE" it passes and I can see it in dev tools.
如果我将“SSID”的密钥更改为例如“TEST_COOKIE”,它就会通过,我可以在开发工具中看到它。 However I need SSID and for some magical reason it doesn't work.
但是我需要 SSID,但出于某种神奇的原因,它不起作用。
Here's example of my code:这是我的代码示例:
class AuthResponse(SuccessResponse):
def __init__(self, data={}, ssid='', access_token: str = '', refresh_token: str = '', **kwargs):
super().__init__(data, **kwargs)
self.set_cookie(key=settings.SESSION_COOKIE_NAME,
value=ssid,)
if access_token:
self.set_cookie(key=settings.ACCESS_KEY_COOKIE_NAME,
value=access_token,)
if refresh_token:
self.set_cookie(key=settings.REFRESH_KEY_COOKIE_NAME,
value=refresh_token,)
AuthResponse inherits from SuccessResponse which is based on DjangoJsonResponse, and DjangoJsonResponse eventually inherits from HttpResponse.
AuthResponse继承自SuccessResponse,它是基于DjangoJsonResponse,而DjangoJsonResponse最终继承自HttpResponse。
So the question is - what could cause of getting rid of "SSID" cookie?所以问题是 - 什么会导致摆脱“SSID”cookie?
I tried to look around and find if all the data appears in init function and apprently eveyrthing is fine.我试着环顾四周,看看是否所有数据都出现在init function 中,显然一切都很好。 All data, ssid, access_token and refresh_token come through, but only "SSID" doesn't get set.
所有数据、ssid、access_token 和 refresh_token 都通过了,但只有“SSID”没有设置。
As well I tried to use "httponly" and "secure" while setting cookies, but it didn't help.我也尝试在设置 cookies 时使用“httponly”和“secure”,但没有帮助。
There was an idea that might be middleware affects somehow on this, however I don't know who to check this out...有一个想法可能是中间件在某种程度上影响了这一点,但我不知道该由谁来检查......
Is there anyone familiar with this who can potentially make an advice of why is this happening?有没有熟悉此情况的人可以就为什么会发生这种情况提出建议?
I found the answer while working on localhost the SESSION_COOKIE_DOMAIN should not be used, so I made it in this way in settings.py:我在 localhost 上工作时找到了答案,不应使用 SESSION_COOKIE_DOMAIN,所以我在 settings.py 中以这种方式实现了它:
if website_settings.current_instance != 'dev':
SESSION_COOKIE_DOMAIN = (
website_settings.session_cookie_domain
if website_settings.session_cookie_domain
else f".{SITE_DOMAIN}"
)
This way it saves all needed cookies and browser sees them.这样它就保存了所有需要的 cookies 并且浏览器可以看到它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.