Reputation: 313
If i clear my cookies and run this view, i get a new session key each page load.
If I comment out the print, and output the session key in the template, new session key each load.
If I print session key in both view and template, the session key is 'saved' and remains the same each page load.
def view_session(request):
print request.session.session_key
return render(request, "view_session.html", {})
So how to explain this behavior?
It is not just two reads, as I can print twice in the view and still get new keys.
Upvotes: 1
Views: 980
Reputation: 174624
By default, Django only saves to the session database when the session has been modified -- that is if any of its dictionary values have been assigned or deleted
To change this default behavior, set the SESSION_SAVE_EVERY_REQUEST setting to True. When set to True, Django will save the session to the database on every single request.
Note that the session cookie is only sent when a session has been created or modified. If SESSION_SAVE_EVERY_REQUEST is True, the session cookie will be sent on every request.
From when sessions are saved.
Upvotes: 3