gpt4 book ai didi

Python - lpthw.web 框架中的异常 "No template named index"

转载 作者:太空狗 更新时间:2023-10-30 02:23:00 24 4
gpt4 key购买 nike

我正在学习一个简短的 Python 教程,但我无法完成最后一个练习。这是app.py的源码

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()

这是 View ,index.html

$def with (greeting)

<html>

<head>

<title>Gothons of Planet Percal #25</title>

</head>

<body>

$if greeting:
I just wanted to say <em style="color: green; font-size: 2em;">
greeting</em>.

$else:
<em>Hello</em>, world!

</body>

</html>

文件app.py在这个目录下:C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\binindex.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/

这是怎么回事,我该如何解决?

提前致谢!

最佳答案

我也有这个问题,但在 OSX 上运行。最终,Zed Shaw 看到了我的求助,也看到了我所犯的错误。

我在运行

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

Zed 提醒我需要运行这个:

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

允许找到模板文件夹。在我这样做之后,它就像一个魅力。

关于Python - lpthw.web 框架中的异常 "No template named index",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7179768/

24 4 0