Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the session key for an anonymous user (django 1.6)

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?

like image 626
thornomad Avatar asked Aug 24 '14 20:08

thornomad


1 Answers

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!

like image 75
thornomad Avatar answered Nov 16 '22 06:11

thornomad