I have added some functionality to the gae-sessions library, so that I can have flash data; That means, data that only exists between 2 requests of a certain user. Here is the code I added:
def set_flashdata(key,val=None):
logging.info('set flashdata '+key+'='+val )
sess = get_current_session()
if val:
sess['flash_'+key]=val
return
for x in key:
sess['flash_'+x]=key[x]
def get_flashdata():
sess = get_current_session()
flash = {}
for key in sess:
if(key.startswith("flash_")):
flash[key[6:]]=sess.pop(key)
logging.info('received flashdata '+key+'='+sess[key])
logging.info('fetched '+str(len(flash))+' flash items')
return flash
def has_flashdata():
sess = get_current_session()
for key in sess:
if key.startswith('flash_'):
return True
return False
but when I run get_flashdata, I get this:
File "/Users/frederikcreemers/Documents/projects/web/myproject/gaesessions/__init__.py", line 533, in get_flashdata
logging.info('received flashdata '+key+'='+sess[key])
File "/Users/frederikcreemers/Documents/projects/web/myproject/gaesessions/__init__.py", line 393, in __getitem__
return self.data.__getitem__(key)
KeyError: 'flash_msg_type'
so, the program sais that the key mg_type is not in sess, but since I’m iterating over the keys of sess, it must be in there.
In the line above, you’ve called
popwhich removes the item from the dictionary.