0

I'am still learning Django/Python and currently try to build an ecommerce site. What I want to do is to save the cart id somewhere. I have read about sessions, but learned that it removes the session_id on login. What I understood is that if a users creates a cart and somewhere in the process decides to login, the cart is lost (that is, the connection between user and cart is lost).

So I thought to use the cookie and use set_cookie("value", card_id). But that requires me to use code like:

response = HttpResponse("Cookie Set")
response.set_cookie('python-tutorial2', 'gktcs.com')
return response

but I have no use for the response since I want to call the cart afterwards.

So, basically, what I am asking is: what is the best way to store the connection between the user and the cart in such a way that it the cart_id is also retrievable if a user logs in (best: even if he logs out)? Or did I miss understand something or is there something else I am missing? Maybe this is a duplicate in which case I probably did not understand what happened there. (I have read 'quit some' in the past days).

Pointers for where to look or how to understand this better are also much appreciated. And I guess there is some security issue as well, but I am not sure.

Quite a big question, sorry for that.

Arie
  • 55
  • 1
  • 8

1 Answers1

1

So, I found 2 solutions.

First, the more generic one is found here: https://stackoverflow.com/a/41849076/517560

Second, it is possible to load the cart_id during the dispatch() of the loginview and set it as a hidden field. Than, while logging in, you get the cart_id from the POST data and create a new session with that cart_id in it.

Arie
  • 55
  • 1
  • 8