I'm looking to disable the automatic session creation in Django for certain URLs. I have /api/* and each client that hits that gets a new Django session. Is there a way to ignore certain urls?
+1
A:
A trivial solution to this is to have your webserver distinguish between API calls and regular calls, then have two different WSGI instances of your application: one with sessions enabled, the other with sessions disabled. (This is probably much easier with Nginx than with Apache.)
An alternative is to inherit SessionMiddleware and then edit the process functions to ignore all requests matching your criteria. Something like:
from django.contrib.sessions.middleware import SessionMiddleware
class MySessionMiddleware(SessionMiddleware):
def process_request(self, request):
if request.path_info[0:5] == '/api/':
return
super(MySessionMiddleware, self).process_request(request)
def process_response(self, request, response):
if request.path_info[0:5] == '/api/':
return
super(MySessionMiddleware, self).process_response(request, response)
And then edit your setting's file so that MIDDLEWARE_CLASSES contains the path to "MySessionMiddleware" and not 'django.contrib.sessions.middleware.SessionMiddleware'.
Elf Sternberg
2010-10-29 17:22:07