【问题标题】:pylons global variable in class类中的 pylons 全局变量
【发布时间】:2012-08-16 13:18:35
【问题描述】:

在 pylons 中,我如何使用在类中被视为全局变量的变量,例如使用 self,在 pylons 中使用 self 似乎不起作用。

假设我在控制器中:

a.py:

class AController(BaseController):

    def TestA(self):
        text = request.params.get('text', None) 
        self.text = text
        redirect(url(controller = 'A', action = 'TestB'))

    def TestB(self):
        render '%s' % self.text

出现错误,'AController' 对象没有属性'text',那么我如何在 pylons 中使用 TestB 显示基于 TestA 的 'text' 或 'self.text'

【问题讨论】:

    标签: class variables pylons


    【解决方案1】:

    我一直在用我的 pylons 控制器做类似的事情。您遇到的问题是,如果您先调用 TestB,则永远不会定义 self.text 。您需要做的就是先定义它。

    这就是我要使您的示例起作用的方法:

    class AController(BaseController):
    
        text = ''
    
        def TestA(self):
            text = request.params.get('text', None) 
            self.text = text
            redirect(url(controller = 'A', action = 'TestB'))
    
        def TestB(self):
            render '%s' % self.text
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 2016-10-06
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      相关资源
      最近更新 更多