我正在看一个简短的Python教程,但最后一个练习无法正常工作。 这是的源代码应用程序副本在
import web
urls = (
'/', 'Index'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class Index(object):
def GET(self):
greeting = "Hello World"
return render.index(greeting = greeting)
if __name__ == "__main__":
app.run()
这是景色,索引.html在
^{pr2}$文件应用程序副本位于以下目录下:C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\bin 以及索引.html位于:C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\templates
因此,当我想运行示例代码时,我在命令提示符中键入以下命令:
C:\Python26\python.exe "C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\bin\app.py"
之后,“http://0.0.0:8080“显示在控制台上,因此我转到浏览器中的http://localhost:8080/ 但我从
<type 'exceptions.AttributeError'> at /
No template named index
Python C:\Python26\lib\site-packages\web\template.py in _load_template, line 992
Web GET http://localhost:8080/
发生了什么事?我该怎么解决它?在
提前谢谢!在
在windows中,文件夹名必须这样写“c:\”而不是“c/”,并且必须使用完整路径。 所以正确的代码是
render = web.template.render('d:\\documents\\python\\templates\\')
(应用程序副本位于d:\documents\python中)我也有这个问题,但是在OSX上运行。最终,泽德·肖看到了我的请求,也看到了我犯的错误。在
我在跑步
Zed提醒我我需要运行这个:
^{pr2}$允许找到templates文件夹。在我这样做之后,它就像一个符咒。在
您有一些键入错误,当您使用
render
(需要与路由的类名相同)时,需要将视图称为Index
:您的
^{pr2}$urls
元组需要一个尾随逗号:还要确保模板名为
Index.html
。不过,看看web.py tutorial,按照惯例,您的route类应该使用小写。在相关问题 更多 >
编程相关推荐