2010-01-12 50 views
1

我已经安装了Pylons v0.9.7,并使用genshi创建了一个项目。 我试图编写一个简单的测试用例,但它不起作用。主塔“全球名称'c'未定义”

代码:member.py

coding: utf-8 
import logging import foo.model 

from foo.lib.base import * 

log = logging.getLogger(__name__) 

class MemberController(BaseController): 

    def index(self): 
     c.title="title" 
     c.mes="message" 
     return render('test.html') 

代码:test.html的

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns:py="http://genshi.edgewall.org/" 
     lang="ja"> 
    <head> 
     <title>${c.title}</title> 
    </head> 
<body> 
    <p>${c.mes}</p> 
</body> 
</html> 

和错误信息(对数)

Error - <type 'exceptions.NameError'>: global name 'c' is not defined 

请帮我找出错误。

回答

3
c.title="title" 

要求名称c定义(全局或本地)。你永远不会定义什么名为c

因此,在给c.title分配任何东西之前,先定义一个合适的名称c(其中属性title可以设置!)。

下一页提示:from pylons import tmpl_context as c - 你没有from ... import ... as,你有没有现在 - )

+1

捐赠是光作为提示? 我有你的书(python CookBook)! 我很荣幸。 此后,它再次尝试。 – Schaft 2010-01-12 05:22:54

+0

啊!有效!谢谢Alex! 我明白“C was tmpl_context”。 我衷心地表示感谢。 – Schaft 2010-01-12 06:24:46