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__":
开发者_Python百科 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?
In the index.py page, should you include from web.template import render
?
Presuming web.template.render()
returns an object containing an index()
method (I'm not familiar with web.py
), you'll need to tell Python where to find this object.
In index.py
:
import app
Then:
return app.render.index(testing)
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.
精彩评论