[Fixed]-Django sessions for anonymous users

11👍

In a past project I did what you are suggesting but just within a view where I needed to use session_key for unauthenticated visitors. It did not cause any problems in my project:

if not request.session or not request.session.session_key:
   request.session.save()

# request.session.session_key now set

-4👍

You can choose to save session every request by setting:

SESSION_SAVE_EVERY_REQUEST = True

This force django to assign the session key for each session

https://docs.djangoproject.com/en/2.1/topics/http/sessions/#when-sessions-are-saved

Leave a comment