【问题标题】:Pylons Mako undefined variablePylons Mako 未定义变量
【发布时间】:2012-08-14 01:52:00
【问题描述】:

在控制器中,我定义了2个方法:

foobar.py:

class foo(self):
    c.help_text = 'help'
    return render('/index.html')

class bar(self):
    return render('/index.html')

index.html:

${c.help_text}

这给了我一个错误 ==> AttributeError: 'ContextObj' object has no attribute 'help_text'

在阅读了一些 mako 文档后,我尝试:

    % if c.help_text is UNDEFINED:
        foo
    % else:
        ${c.help_text}
    % endif

它也给了我一个错误。然后在我的 development.ini 中,我输入:

mako.strict_undefined = false

之后

[app:main]

这仍然给我一个错误 ==> AttributeError: 'ContextObj' object has no attribute 'help_text'

【问题讨论】:

标签: python pylons mako


【解决方案1】:

我认为您的控制器代码不正确。您的第一个样本应该是...

def foo(request):
    c.help_text = 'help'
    return render('/index.html')

def bar(request):
    return render('/index.html')

...或...

class Controller(object):
    def foo(self, request):
        c.help_text = 'help'
        return render('/index.html')

    def bar(self, request):
        return render('/index.html')

我相信由于您的控制器代码不正确,“c.help_text”实际上并没有在响应处理查询时运行,而是在您启动应用程序时正确处理。

如果您修复了这些错误但仍有问题,您能否提供更多有关错误的信息?您有堆栈跟踪或确切的行号吗?

【讨论】:

    猜你喜欢
    • 2012-08-13
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 2011-04-21
    • 2012-04-21
    • 2021-02-27
    • 2011-03-06
    相关资源
    最近更新 更多