It's not putting anything into the session. The session is basically a jazzed-up dictionary, and the get method works pretty much just like it does with the dictionary, it will try and get the objected pointed to by the key (the first argument) but if that doesn't work, it returns the default (the second argument) instead. Just like with a dictionary, there is no implicit assignment. If you want to assign a value to the entry in the session, you have to do so explicitly.
The expression: cart = request.session.get('cart', None) or Cart() tries first to look for the key 'cart' in the session dictionary. If it is there, because of the short-circuit logic of the or operator, the Cart() never gets evaluated. If the key isn't in the dictionary, the get returns None and the part after the or gets evaluated, which results in a new Cart getting constructed and assigned to cart. Then if you want to save that object and put it in the session, you have to cart.save() and request.session['cart'] =cart just like you have in your get_cart method. Stephen On Fri, Mar 13, 2009 at 5:02 PM, sotirac <soti...@gmail.com> wrote: > > Looking at the django-cart open source code: > > cart = request.session.get('cart', None) or Cart() > > Does the new cart object that recently got created be put back into > request.session['cart']. > > I was thinking of improving the code located in > > http://code.google.com/p/django-cart/source/browse/trunk/django-cart/shop/views.py > by adding a get_cart() > > def get_cart(request): > cart = request.session['cart'] > if cart == None > cart = Cart() > cart.save() > request.session['cart'] = cart > return cart > > Is there a cleaner way of doing this. > > Thanks in advance. > > On Mar 13, 3:57 pm, sotirac <soti...@gmail.com> wrote: > > What is happening here: > > > > fav_color = request.session.get('fav_color', 'red') > > > > Is it trying to retrieve 'fav_color' from the session. If it doesn't > > exist, does it put it in the session and assign the value 'red' for > > it. > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---