开发者

Python - Exception "No template named index" in lpthw.web framework

I am going through a short Python tutorial, but I can't get the last exercise to work. This is the source code of 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() 

and this is the 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> 

The file app.py is under this directory: C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\bin and index.html is at: C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\templates

So, when I want to run the sample code, I type this in the command prompt:

C:\Python26\python.exe "C:\Users\Lucas\Desktop\Learn Python The Hard Way\ex50\gothonweb\bin\app.py"

After that, "http://0.0.0:8080" is displayed on the console, so I go to http://localhost:8080/ in my browser bu开发者_如何学编程t I get back a long traceback starting with

<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/

What is going on and how do I fix it?

Thanks in advance!


I had this problem as well but running on OSX. Ultimately Zed Shaw saw my pleas for help and saw the mistake I was making.

I was running

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

Zed reminded me that I needed to be running this:

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

to allow the templates folder to be found. After I did that it worked like a charm.


in windows ,the folder'name must write like this "c:\" not "c/",and you must use full path. so the right code is render = web.template.render('d:\\documents\\python\\templates\\') (app.py is in d:\documents\python)


You have a few typos, you need to refer to your view as Index when you use render (needs to be the same as the class name for your route):

return render.Index(greeting = greeting)

And your urls tuple needs a trailing comma:

urls = (
    '/', 'Index',
)

Also make sure your template is named Index.html. Although, looking at the web.py tutorial, it looks like by convention you'd use lowercase for your route class.


Well, I suffered from the same problem, and I must say the error message is right, which indicates "you" cannot find the file, simply because you are not in the right path. So @davidheller @shellfly are right.

I use PyCharm as IDE to write python, so here is my solution: Since I run the app.py, which is under the bin directory, thus render = web.template.render('../templates/') which .. goes up and then found the file.

To conclude, we must be sure about the current path(even in windows), and both relative path or absolute path can be used in Windows environment, as shown below:

  1. Absolute path.

    Absolute path. Since Windows accepts both "/" and "\", we can write

    render = web.template.render('C:/Users/Lucas/Desktop/Learn Python The Hard Way/ex50/gothonweb/templates/')
    

    or

    render = web.template.render('C:\\Users\\Lucas\\Desktop\\Learn Python The Hard Way\\ex50\\gothonweb\\templates\\')
    

    Note, python interpreter interprets "\\" as "\"

  2. Relative path.

    render = web.template.render('../templates/')
    


You may need to compile the template like so

python web/template.py --compile templates

anyone using web.py with the Google app engine you will need to.


I am doing the same exercise and I simply go on cmd , cd to my lpthw directory which contains the folders of the project skeleton inside and do:

> python bin/app.py 

I think you have to put all your files from the project skeleton in one folder and run your app from there. Hope this helps. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜