I wrote a webservices application over django. This application can be found as several instances on our servers.
For example:
http://server1/testapp/
http://server2/sandboxapp/
http://server2/productionapp/
The app instances are hosted on apache2 through mod_wsgi.
I use cookie authentication for all of these instances, and would like the cookies for each not to overlap with the others. I need to set the SESSION_COOKIE_PATH value.
It happens that I don’t know beforehands the URLs behind which the application is deployed. So I’d like to use a reverse url to guess that value.
My main urls.py looks like this:
urlpatterns = patterns('',
(r'admin/', include(admin.site.urls)),
url(r'^$', view_server_root, {}, name='server_root'),
),
# here I also includes urls from the various sub-apps
for app in apps:
urlpatterns.append(url(urlprefix, include(app.urls))
I added this bit after all urls are loaded:
from django.core import urlresolvers
from django.conf import settings
settings.SESSION_COOKIE_PATH = urlresolvers.reverse('server_root')
Unfortunately urlresolvers.reverse('server_root') returns / and not the expected, for example, /testapp/. My cookies overlap if I log in to each instance.
Though, later on in the application usage, I use django.db.models.permalink for some views, which returns me a correct prefixed URL (e.g. /testapp/page/465).
There may be something with the context that is missing while executing urls.py.
Any ideas?
Thanks for reading.
You shouldn’t change Django settings at runtime. If you’re using Django 1.4 or later, you might be able to use
reverse_lazyand setSESSION_COOKIE_PATHin your settings.py.