in my current django project, I am using request.session[‘key’] for obtaining cookies from users. However there is a problem, I want to check if a user is at my website for a first time.
This has been done with:
if not request.session.get('visited', False):
# do something
request.session['visited'] = True
This code works fine, however what does not work is when a user logs out:
auth.logout(request)
I looked at the django code for this specific method, and to no surprise, it flushes the user’s session, which is where my visited data is stored.
As a result, when users log out, they see my welcome message again.
How can I keep a key/value pair that is safe from the auth.logout flush?
Thank you!
You need to use cookies and not django sessions. A session expires once a user logs out.
What you need to do is set a cookie using the set_cookie method and then test for is existence using request.COOKIES.get()
Also please take a look at this https://docs.djangoproject.com/en/dev/ref/request-response/
Let me know if you still have issues.