Skip to main content
 首页 » 编程设计

python之Pylons Mako undefined variable

2025年05月04日8kenshinobiy

在 Controller 中,我定义了 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'

请您参考如下方法:

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

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') 

我相信因为您的 Controller 代码不正确,“c.help_text”实际上并没有运行以响应处理查询,而是在您启动应用程序时立即处理。

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