13

Am trying to keep track of AnonymousUsers by their session information (if possible).

In older versions of Django, I could do something like:

def my_view(request): # in case the user wasn't logged in, create/save a session if not request.session.session_key: request.session.save() # would give me the key and on the next load it would persist session_key = request.session.session_key 

But with 1.6 (and I've been out of the game for a while) this results in a new unique session ID each time the request is put through. There is no persistence. I've tried to do a little reading but am going in circles as I'm out of Django practice.

How do I have a session persist? Do I need to write my own cookie handling?

2
  • [django session key changing upon authentication][1] [1]: stackoverflow.com/a/14757138/1978033 Commented Aug 25, 2014 at 0:33
  • Thanks for the link - tried overriding the cycle_key method but it doesn't appear that is being called at all when an anonymous user refreshes a page ... at each refresh I am getting an empty session_key. Commented Aug 25, 2014 at 1:57

1 Answer 1

9

So, after I started reading through the source code I found myself on the global_settings.py file and found this gem:

SESSION_SAVE_EVERY_REQUEST = True 

When I added that to the settings.py file my problems were solved. AnonymousUsers got a session_key. Yipee!

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.