This article mainly introduces how to enable django applications with different ports on the same server to be opened in the same browser. if we have two django applications site1 and site2 running on different ports of the same server at the same time, at the same time, we log on to different tabs in the same browser. In this case, when we log on to site2, the user logged on to site1 will be kicked down.
Why is this happening? This is related to the django session framework. here is a brief introduction: when we visit a django website for the first time, django will generate a session to save some information about the current session. At the same time, a hash value session_key is generated and a cookie is generated and sent to the client. the cookie name is set according to SESSION_COOKIE_NAME in setting. the default value is "sessionid" (highlighted ). In this way, the next request for session_key will be sent to the server following the cookie. Server searches for the corresponding session object based on session_key to obtain information about the current session, including logon information.
So there is only one truth in the above situation (Ke Nan pushes glasses face ):
Log on to site1 to get a cookie named sessionid, which stores session_key1.
When we log on to site2, we will update the cookie named sessionid. now its value is session_key2 (the cookie stored in the browser is based on ip rather than Port, so the cookie with the same name will be updated ).
Therefore, when you use the new session_key to access site1, you will not be able to obtain the original logon information. you need to log on again.
So how can we solve this problem? after understanding the above mechanism, you only need to set SESSION_COOKIE_NAME in setting. For example, you can set SESSION_COOKIE_NAME = 'site2' in site2, and use the default value in site1. You can also set site1 and site2 at the same time.
For more details, you can open relevant articles in the same browser for django applications with different ports on the same server. please follow the PHP Chinese network!