Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

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 but 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!

like image 857
Lucas Avatar asked Aug 24 '11 17:08

Lucas


2 Answers

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.

like image 182
davidheller Avatar answered Sep 20 '22 18:09

davidheller


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)

like image 2
shellfly Avatar answered Sep 22 '22 18:09

shellfly