2012-09-02 18 views
0

新手问题。AppEngine Python - 如何处理缓存的对象值

所以我有这个处理程序的页面附加一些CSS/JS文件。问题在于后续的请求会导致一次又一次地附加值。

例子:

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) 

你通常如何在像谷歌的AppEngine平台处理这个问题,因为我是从PHP的背景,其中仅对象在当前请求中生活的到来。

感谢

按照要求,这里是基类:

基地控制器

from google.appengine.ext.webapp import template 

import os 
import config 
import datetime 

class BaseController(object): 

    request = None 
    response = None 
    action = 'index' 
    method = 'get' 
    params = [] 
    template_values = {} 
    template_dir = None 
    template = None 

    default_styles = ['/media/bootstrap/css/bootstrap.min.css', '/media/css/style.css'] 
    default_scripts = ['/media/js/jquery-1.6.4.min.js'] 

    styles = [] 
    scripts = [] 

    def __init__(self, request, response, *args, **kwargs): 
     self.request = request 
     self.response = response 

     self.action = 'index' 
     if 'action' in kwargs and kwargs['action']: 
      self.action = kwargs['action'] 

     self.method = 'get' 
     if 'method' in kwargs and kwargs['method']: 
      self.method = kwargs['method'] 

     self.params = [] 
     if 'params' in kwargs and kwargs['params']: 
      if isinstance(kwargs['params'], list): 
       self.params = kwargs['params'] 

     # Initialize template related variables 
     self.template_values = {} 
     self.styles = list(self.default_styles) 
     self.scripts = list(self.default_scripts) 

    def pre_dispatch(self): 
     pass 

    def post_dispatch(self): 
     if self.template is not None and self.template_dir is not None: 
      # Populate current year 
      dt = datetime.date.today() 
      self.template_values['current_year'] = dt.year 

      # Populate styles and scripts 
      self.template_values['styles'] = self.styles 
      self.template_values['scripts'] = self.scripts 

      path = os.path.join(config.template_dir, self.template_dir, self.template) 
      self.response.out.write(template.render(path, self.template_values)) 

    def run_action(self): 
     action_name = 'action_' + self.action 
     if hasattr(self, action_name): 
      action = getattr(self, action_name) 
      action() 
     else: 
      raise Http404Exception('Controller action not found') 

    def dispatch(self): 
     self.pre_dispatch() 
     self.run_action() 
     self.post_dispatch() 

class HttpException(Exception): 
    """Http Exception""" 
    pass 

class Http404Exception(HttpException): 
    """Http 404 exception""" 
    pass 

class Http500Exception(HttpException): 
    """Http 500 exception""" 
    pass 

而子类

import datetime 

from dclab.lysender.controller import BaseController 

class ProjectsController(BaseController): 

    template_dir = 'projects' 

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

我的错是,我指定一个列表通过引用到另一个列表,而不是克隆列表。我并不清楚这种行为,这就是为什么它让我整晚都在挠头。

+0

看起来像引用任务咬我。我大喊写self.scripts = list(self.default_scripts),而不是,这是否适用于所有python对象? – Lysender

+0

显示实际的课程?看起来你想在构造函数中做这些事情,而不是无论这个action_index()方法是什么。这与你的实例无论如何被缓存有关,但没有足够的信息。 – dragonx

+0

嗨gragonx,我通过克隆列表解决了这个问题,我不知道这个参考事情,所以这就是问题所在。上面的代码已经在工作。只是改变了名单的事情。 – Lysender

回答

2

你在类定义下直接声明了很多变量。这样做并没有像你期望的那样定义实例成员,而是定义了类变量 - 相当于像Java这样的语言中的“静态”。

而不是将事情声明为类变量,在构造函数中初始化您的值 - 您的类的__init__方法。

我也强烈建议使用像webapp2现有的Web框架,而不是发明自己的。

+0

是的,这正是我正在做的,将我的小应用程序/网站迁移到webapp2并垃圾我自己的代码。使用__init__而不是重写dispatch()会更好吗?无论如何,这真的有帮助。我仍然会研究re:实例成员vs类变量。 – Lysender

+0

@Lysender你的代码没有使用webapp - 你似乎在发明自己的请求处理程序('BaseController')。无论你使用'__init__'还是'__dispatch__',都取决于你想要做什么。 –

+0

我没有显示我的完整代码,它确实使用webapp(而不是webapp2)。我创建了自己的webapp上的请求处理程序,用于某种自动路由,自动模块导入等(与流行的PHP框架相同)。但是,当我看到webapp2时,我意识到webapp2真的是我需要的。 – Lysender