python - Weird session object behavior in django -


I try to apply a simple train using a dictionary

{ITEM_ID: quantity} I have been a list of items {{ID: Quantity}} dajax Quantity: (i.e., idem_id): ITEM_ID = integer (ITEM_ID) print "Itemid:", ITEM_ID # = 'Dajax' (if not 'in the cart' request.session: request.session ['train'] = {} train = request.session ['train'] If ITEM_ID is not in the car: car [ITEM_ID] = 0 car [ITEM_ID] ] = 1 print "cart:", train request.session ['train'] = train request.ses Sion.modified = true request.session.save () count = amount (cart.values ​​()) dajax.assign ('# cart_items', 'innerHTML', str (number))

I did not use any thing as a try, as mentioned in the document, I received a very strange result request.session.modified

  Cart: {u'1 ' : 1, 1: 1, u'3 ': 1, u'2': 1}   

Note: I explicitly rounded up the ITEM_ID, so I do not know Where is the string key coming from!

Note: The cart object does not hold more than 4 keys!

The default syntax is JSON format used by sessions

JSON Only allows the key in the form of a string, so when the data is saved in the database, all the integer keys are converted into a string. When you load the data, the fact that they have lost the integer, and the keys are plain strings (well, unicode wire).

The simplest solution is to use the string in particular in its session data keys, and only convert it to the integer for the further process.

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -