views:

761

answers:

5

I have two Django projects and applications running on the same Apache installation. Both projects and both applications have the same name, for example myproject.myapplication. They are each in separately named directories so it looks like .../dir1/myproject/myapplication and .../dir2/myproject/myapplication.

Everything about the actual public facing applications works fine. When I log into either of the admin sites it seems ok, but if I switch and do any work on the opposite admin site I get logged out of the first one. In short I can't be logged into both admin sites at once. Any help would be appreciated.

A: 

Well, if they have the same project and application names, then the databases and tables will be the same. Your django_session table which holds the session information is the same for both sites. You have to use different project names that will go in different MySQL (or whatever) databases.

Gabriel Ross
A: 

The session information is stored in the database, so if you're sharing the database with both running instances, logging off one location will log you off both. If your circumstance requires you to share the database, the easiest workaround is probably to create a second user account with admin privileges.

Jeff Bauer
+6  A: 

Set the SESSION_COOKIE_DOMAIN option. You need to set the domain for each of your sites so the cookies don't override each other.

You can also use SESSION_COOKIE_NAME to make the cookie names different for each site.

strager
When I set the option to different domains for each site (.mysite1.com, .mysite2.com) and try to login, I get an error about my browser not being set up to accept cookies. When I remove it reverts back to original behavior.
When I used SESSION_COOKIE_NAME instead of DOMAIN it seemed to fix my problem. This is all running on my localhost.
When I meant to set the doman, I meant to set the path, e.g. "/site1" and "/site2". Having different cookie names will also resolve the conflict. Remember, though, that cookies can still conflict if they are in the same namespace.
strager
A: 

Let me guess, is this running on your localhost? and you have each site assigned to a different port? i.e. localhost:8000, localhost:8001 ..?

I've had the same problem! (although I wasn't running Apache per se)

When you login to the admin site, you get a cookie in your browser that's associated with the domain "localhost", the cookie stores a pointer of some sort to a session stored in the database on the server.

When you visit the other site, the server tries to interpret the cookie, but fails. I'm guessing it deletes the cookie because it's "garbage".

What you can do in this case, is change your domain

use localhost:8000 for the first site, and 127.0.0.1:8001 for the second site. this way the second site doesn't attempt to read the cookie that was set by the first site

I also think you can edit your HOSTS file to add more aliases to 127.0.0.1 if you need to. (but I haven't tried this)

hasen j
A: 

I ran into a similar issue with a live & staging site hosted on the same Apache server (on CentOS). I added unique SESSION_COOKIE_NAME values to each site's settings (in local_settings.py, create one if you don't have one and import it in your settings.py), set the SESSION_COOKIE_DOMAIN for the live site and set SESSION_COOKIE_DOMAIN = None for staging. I also ran "python manage.py cleanup" to (hopefully) clean any conflicted information out of the database.

Tom