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?

lucasnadalutti
  • 5,818
  • 1
  • 28
  • 48
thornomad
  • 6,707
  • 10
  • 53
  • 78
  • [django session key changing upon authentication][1] [1]: http://stackoverflow.com/a/14757138/1978033 – John Aug 25 '14 at 00: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. – thornomad Aug 25 '14 at 01:57

1 Answers1

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!

thornomad
  • 6,707
  • 10
  • 53
  • 78