[Answered ]-Django enable anonymous user session

2👍

if not request.user.is_authenticated():
    # create your session data for anonymous user

but I would not save cart data into session. I would save them into database

0👍

just you can use the following code inside your view:

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

if the user is anonymous(not logged in user) the session id will created, and you can add your cart items (or anything you want ) in this session , and after the user will logged-in to your system, the session id will remines the same .

i hope this helpful .

👤K.A

Leave a comment