中的Python异常“没有名为index的模板”lpthw.web网站故障估计机

2024-09-24 02:18:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在看一个简短的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/

发生了什么事?我该怎么解决它?在

提前谢谢!在


Tags: thewebapphttptemplaterenderlearnusers
3条回答

在windows中,文件夹名必须这样写“c:\”而不是“c/”,并且必须使用完整路径。 所以正确的代码是render = web.template.render('d:\\documents\\python\\templates\\') (应用程序副本位于d:\documents\python中)

我也有这个问题,但是在OSX上运行。最终,泽德·肖看到了我的请求,也看到了我犯的错误。在

我在跑步

~/projects/gothonweb/bin $ python app.py

Zed提醒我我需要运行这个:

^{pr2}$

允许找到templates文件夹。在我这样做之后,它就像一个符咒。在

您有一些键入错误,当您使用render(需要与路由的类名相同)时,需要将视图称为Index

return render.Index(greeting = greeting)

您的urls元组需要一个尾随逗号:

^{pr2}$

还要确保模板名为Index.html。不过,看看web.py tutorial,按照惯例,您的route类应该使用小写。在

相关问题 更多 >