[Solved]-How to share session between two django application?


Other than you have to apply these settings to both applications,
the only thing missing with your approach is the SESSION_COOKIE_DOMAIN.

You set it to ‘.abc.com’, which means it will work if your app has domain name: www.abc.com and somesubdomain.abc.com.

But your second app in this case www.abc.com:9002, by including the port it doesn’t share the same TLD with www.abc.com. So, django thinks www.abc.com:9002 and www.abc.com are very different domain and not from the same root .abc.com.

If I’m working on this, there are several possible approach:

  1. Combine both app into one single root django app. Django app were modular anyway, so you could create one single ROOT_URL_CONF and DJANGO_SETTINGS_MODULE to specify how these two apps works in the same domain. You could, for example, append a different prefix url for each app.

  2. You use load balancer, or reverse proxy, such as nginx or haproxy to assign different subdomain for each app, then deploy each app in a different port. Let’s say, the end result is you have the first django app deployed on first.abc.com and the second app in second.abc.com (All with port 80 in the frontend), then it will share the same session. Just remember that in the backend you need to assign the actual port that the app uses.

Additional notes from mine. In production settings, you also want to add ALLOWED_HOSTS settings and include .abc.com in the list.

Leave a comment