将子域用于Django站点时,如何在localhost上的子域之间共享django登录? [英] When using sub-domains for a Django site, how can you share django logins across sub-domains on localhost?
问题描述
我想让同一用户会话跨越:
site.com
sub1.site.com
sub2.site.com
如何在Django中做到这一点?使用默认的auth用户软件包,似乎要求用户每次使用不同的会话登录所有3个站点。他们如何共享相同的登录Cookie和会话ID?
I want to let the same user session span across:
site.com
sub1.site.com
sub2.site.com
How can I do this in Django? With the default auth user package it seems to require the user to login to all 3 sites each time with a different session. How can they share the same login cookie and session-id?
更新:在settings.py中使用SESSION_COOKIE_DOMAIN值似乎可以在生产站点上使用,但不能使用在localhost / dev服务器上为我工作。如何使它适用于localhost子域?当我将SESSION_COOKIE_DOMAIN更改为生产网站名称或 .localhost时,django auth登录完全停止工作(我无法登录,在localhost上没有创建cookie。)
UPDATE: Using the SESSION_COOKIE_DOMAIN value in settings.py seems to work on production sites, but it doesn't work for me on localhost/dev servers. How do you get it to work for localhost sub-domains? When I change the SESSION_COOKIE_DOMAIN to the production website name or ".localhost" django auth logins completely stop working (I'm unable to ever login, no cookie is created on localhost.)
推荐答案
我认为我有一种解决方法,但是无法使用localhost。我只能在映射到127.0.0.1的测试 .com域上使用它。
在我的/ etc / hosts文件中(在OSX上)
I think I got a workaround solution, but couldn't use localhost. I could only get it working for a test ".com" domain that maps to 127.0.0.1.
In my /etc/hosts file (on OSX:)
127.0.0.1 test.com
127.0.0.1 sub1.test.com
127.0.0.1 sub2.test.com
然后在我的开发设置上。py:
Then on my development settings.py:
SESSION_COOKIE_DOMAIN=".test.com"
我无法使用纯 localhost工作,看来我需要在那里输入 .com字符串即可正常工作。因此,我可以在浏览器中使用sub1.test.com:8000和sub2.test.com:8000登录并拥有跨子域身份验证Cookie。
I could not get this working with plain "localhost", it seemed I needed the ".com" string in there to get it working. So then I could login and have cross subdomain auth cookies using sub1.test.com:8000 and sub2.test.com:8000 in my browser.
这篇关于将子域用于Django站点时,如何在localhost上的子域之间共享django登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!