Python

AppEngine – Python – Caches Object Values

[UPDATE]: Solved this problem, explanation at the bottom.

So I have this semi-MVC AppEngine for Python setup where I setup different CSS/JS files per page. The problem is that AppEngine caches object values causing values to stack. Example, if I add a CSS file to a list, the next request will add it again causing more files added.

Maybe I was just used to the usual PHP setup where all objects and variables exists only on the current request. With this Google AppEngine stuff, forget the PHP world. Below is my code.

def action_index(self):
    self.template = 'index.html'
    extra_styles = ['/media/css/jquery.lightbox-0.5.css']
    extra_scripts = ['/media/js/jquery.lightbox-0.5.min.js', '/media/js/project.js']

    for style in extra_styles:
        self.styles.append(style)

    for script in extra_scripts:
        self.scripts.append(script)

Right now I’m still scratching my head.

[UPDATE]: The culprit of the issue above is the fact the lists when assign to another variable simply copies the reference, not the value. Therefore, if the new variable is modified, the original variable also gets modified, hence the bug.

However, the biggest fix I’ve done is the move to webapp2. Here is the detail of the migration.

Leave a reply

Your email address will not be published. Required fields are marked *