Django 1.0 (or trunk r9029) has a bug that does not store any session data using file-based sessions on Windows system.
[1] Sample code
To use file-based sessions, set the SESSION_ENGINE setting to "django.contrib.sessions.backends.file". On Windows system, the following code does not work properly. The session data for "sess_key" will never be passed to the next index2 session.
---[ views.py ]---
def index(request):
request.session['sess_key'] = "yes"
request.session.modified = True
return HttpResponseRedirect('index2.html')
def index2(request):
print "session key="+repr(request.session['sess_key'])
return HttpResponseRedirect('index3.html')
---
[2] My analysis
This is a bug of save() function of django.contrib.sessions.backends.file.SessionStore? class. On Windows, os.rename function of Python will raise OSError if the destination file already exists. Whereas on Unix, it will be removed silently. To fix this problem, remove the existing file before renaming if the current operating system is Windows.
[3] My workaround
Apply unlink() function before rename() if the operating system is Windows to fix the save() function of file.SessionStore? class.
---[ django/contrib/sessions/backends/file.py ]---
def save(self, must_create=False):
...
try:
...
#00122:
+ if os.name == "nt":
+ os.unlink(session_file_name)
os.rename(output_file_name, session_file_name)
renamed = True
---
# added patch code with "+" mark