0

新手问题。

所以我有这个处理程序用于附加一些 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)

您通常如何在 Google 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'
        ])

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

4

1 回答 1

2

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

不要将事物声明为类变量,而是在构造函数中初始化你的值——__init__你的类的方法。

我还强烈建议使用现有的 web 框架,如 webapp2,而不是自己发明。

于 2012-09-02T21:17:14.167 回答