简体   繁体   中英

gae-sessions not deleting data

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.

在上面的行中,您调用了pop ,它从字典中删除了该项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM