Web.py 在这种情况下如何访问渲染函数
我是 Python 和 Web.py 的新手,但我对这个问题感到抓狂。我有一个代码布局,我的 app.py
文件位于网站的根目录中。所有页面都位于一个名为 pages
的子目录中。这是我的 app.py 代码
import web
import page.index
urls = (
'/', 'page.index.index',
)
render = web.template.render('templates/', base = "layout") # Start the template
app = web.application(urls, globals()) # Start the app
if __name__ == "__main__":
app.run()
现在,它执行得很好。现在,在index.py文件中,这是我的代码:
class index:
def GET(self):
testing = 'Hello World'
return render.index(testing)
我得到的错误是这样的:
<type 'exceptions.NameError'> at /
global name 'render' is not defined
Python /Volumes/Local Disk 2/Work/Casting Board/com/index.py in GET, line 3
Web GET http://127.0.0.1:8080/
基本上,我正在尝试访问该函数(或者它是方法或类。刚刚来自PHP,所以不知道终端) 从名为 page.index 的模块render
。我该如何解决这个问题?
I am new to Python and Web.py, but I am tearing my hair out over this issue. I have a code layout where I have my app.py
file in the root of my site. All the pages are in a sub director, named pages
. Here is my app.py code
import web
import page.index
urls = (
'/', 'page.index.index',
)
render = web.template.render('templates/', base = "layout") # Start the template
app = web.application(urls, globals()) # Start the app
if __name__ == "__main__":
app.run()
Now, it executes perfectly. Now, in the index.py file, this is my code:
class index:
def GET(self):
testing = 'Hello World'
return render.index(testing)
The error I am getting is this:
<type 'exceptions.NameError'> at /
global name 'render' is not defined
Python /Volumes/Local Disk 2/Work/Casting Board/com/index.py in GET, line 3
Web GET http://127.0.0.1:8080/
Basically, I am trying to access the function ( or it is method or class. Just coming from PHP so don't know the terminally) render
from a moucle called page.index. How can I get around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在index.py页面中,是否应该包含
from web.template import render
?In the index.py page, should you include
from web.template import render
?假设
web.template.render()
返回一个包含index()
方法的对象(我不熟悉web.py
),你需要告诉 Python 在哪里可以找到这个对象。在
index.py
中:然后:
Presuming
web.template.render()
returns an object containing anindex()
method (I'm not familiar withweb.py
), you'll need to tell Python where to find this object.In
index.py
:Then:
您只需在 app.py 文件中包含索引类 (index.py) 即可。然后导入 Web 模块将在代码开头引入函数“render”的定义。
You can just include you index class (index.py) within your app.py file. Importing the web module then will bring in the definition of the function "render" at the beginning of your code.