template problem: could not find 'def' (line 1)

8 views
Skip to first unread message

Flywood

unread,
Mar 14, 2008, 2:27:57 AM3/14/08
to web.py
Hi,
I'm newbie to web.py and I met my first problem in the tutorial
example. I always got NameError "could not find 'def' (line 1)' when I
intended to use the template. here is my code and template.

code.py
-------------------------------------------------------------------------
#!/usr/bin/env python

import web
render=web.template.render('templates/',cache=False)

urls=('/(.*)','index')

class index:
def GET(self,name):
print render.index(name)


web.webapi.internalerror=web.debugerror

if __name__=="__main__":
web.run(urls,globals(),web.reloader)
--------------------------------------------------------------------------------

here is my template 'index.html' under directory 'templates/':
----------------------------------------------------------------------
$def with (name)

Hello!,<em>$name</em>
----------------------------------------------------------------------

when I access the link 'http://localhost:8080/sam' in browser, below
msg displayed:

---------------------------------------------------------------
<type 'exceptions.NameError'> at /sam
could not find 'def' (line 1)
Python C:\Python25\Lib\site-packages\web\template.py in h_var, line
674
Web GET http://localhost:8080/sam
----------------------------------------------------------------

anyone can help me out of this problem? thanks a lot!

Aaron Swartz

unread,
Mar 14, 2008, 2:29:43 AM3/14/08
to we...@googlegroups.com
You need to save your templates with Unix-style file endings (these
are the CRLF settings).

Flywood

unread,
Mar 14, 2008, 2:45:24 AM3/14/08
to web.py
Hi, Aarom
thanks your reply. I had tried to save my templates with Unix-format
and windows-format, but got the same results :(

BTW: I run web.py in Cygwin environment in WINXP.

Juan Pablo Scaletti

unread,
Mar 14, 2008, 10:45:30 AM3/14/08
to web.py
Check your template. The "$def with (name)" part must be complete un-
indented, meaning "$" must be the first character of the line.
Seems to me that in your template isn't.

Sekhri Lotfi

unread,
Mar 15, 2008, 6:53:25 PM3/15/08
to web.py
Hello,
I think that you must install " Cheetah ", to make working the
templates.
from the official site:
www.cheetahtemplate.org
or with the command line (with easy_install):
# easy_install Cheetah

try to do this ;-)
> Web GEThttp://localhost:8080/sam

Juan Pablo Scaletti

unread,
Mar 16, 2008, 1:23:44 AM3/16/08
to web.py
He is using "templetor" (web.py's template system) not cheetah.

The "could not find 'def' (line 1) " error appears when the template
has a space before the "$def with name".

blaamann

unread,
Mar 16, 2008, 6:57:17 AM3/16/08
to web.py
On Mar 16, 6:23 am, Juan Pablo Scaletti <juanpablo.scale...@gmail.com>
wrote:
> He is using "templetor" (web.py's template system) not cheetah.
>
> The "could not find 'def' (line 1) " error appears when the template
> has a space before the "$def with name"

Yes, there is a space before the $def and it will result in that
error, but another weird thing about that example is that there is no
value for name. It is just passed to the template. Add name="Tom" to
make it more sensible for a first web.py application.

class index:
def GET(self,name):
name = "Tom"
print render.index(name)


Cheers

bubblboy

unread,
Mar 16, 2008, 8:37:09 AM3/16/08
to we...@googlegroups.com

Ehh.. what? There is a value for name; it is passed as an argument?

blaamann

unread,
Mar 16, 2008, 9:04:10 AM3/16/08
to web.py
> > class index:
> > def GET(self,name):
> > name = "Tom"
> > print render.index(name)
>
> > Cheers
>
> Ehh.. what? There is a value for name; it is passed as an argument?

You are right. My mistake.

Flywood

unread,
Mar 17, 2008, 9:57:23 PM3/17/08
to web.py
Thank you all for the valuable comments. :) I had found the cause for
this problem: I saved my template with UTF-8 format. it seems that the
web.py doesn't decode it right and think there was other non-printed
char before "$def with name". after I saved it with ansi format. the
problem gone.

so may I have the conclusion that the web.py doesn't support UTF-8/
Unicode well?



On 3月16日, 下午1时23分, Juan Pablo Scaletti <juanpablo.scale...@gmail.com>
wrote:

Aaron Swartz

unread,
Mar 17, 2008, 10:20:34 PM3/17/08
to we...@googlegroups.com
Hmm, sounds like a BOM issue. I'll file a bug.

lui...@gmail.com

unread,
Mar 23, 2008, 5:59:15 PM3/23/08
to web.py
This problem is getting on my nerves...
I can't find a way to save my templates in unix format with the text
editors I have installed (notepad, wordpad, word, idle).
Is there any way, if not with an editor at least programmatically, to
save a file as unix-style CLRF?

Hermann Kaser

unread,
Mar 23, 2008, 6:01:41 PM3/23/08
to we...@googlegroups.com
I don't know about any of those, but try Ultraedit. As far as I know
it'll let you do anything with a text file.

--
Hermann Käser
http://theragingche.com/
http://semicir.cl/user/hermzz

lui...@gmail.com

unread,
Mar 23, 2008, 6:38:38 PM3/23/08
to web.py
I'll try UltraEdit, thanks!
By the way, I just used Word to save it as us-ascii CLRF, and it works
when used with the built-in-server, but not when used with Apache &
mod_wsgi.

Anyone knows why?

bubblboy

unread,
Mar 23, 2008, 6:53:03 PM3/23/08
to we...@googlegroups.com

Of course: vim [1] :) Use the :set filetype=unix option. I don't know
how much you know about vi(m) and how much programming you do, but in
case you're still actually using notepad/ultraedit/etc; vim is
definitely in the top-2 of programmers worldwide's favourite editors
(along with emacs). You might want to give it a serious try (steep
learning curve but, at least according to many, worth it). Or try emacs,
that too, has many fans.

Not trying to start an editor-war here, I was just a little worried that
this poor devil was actually programming in notepad.

Good luck,

b^4

(PS: it's CRLF; "carriage return" and "line feed" ;))

[1] http://vim.sourceforge.net/

lui...@gmail.com

unread,
Mar 23, 2008, 7:02:28 PM3/23/08
to web.py
Well actually this poor devil uses good old IDLE for python, but I
guess it's time to check other alternatives...:-)
(As for CRLF, you're right. I just made a bad translation from
spanish...).



On Mar 23, 7:53 pm, bubblboy <bubbl...@gmail.com> wrote:

Eric

unread,
Mar 26, 2008, 8:35:44 PM3/26/08
to web.py
If you happen to have Cygwin available (you didn't mention that you
did), the command-line tool dos2unix will do the trick. (Sometimes
it's called d2u.) Or, if you're pushing your files around by FTP, look
at your FTP program's options for converting/preserving line endings.

Some Unix-oriented programs assume Unix-style line endings and choke
on <CR><LF>, while others will attempt conversions or just ignore the
<CR> as meaningless whitespace and keep going. I don't know how Apache
+ mod_wsgi prefers it, but that could be the source of what you're
seeing.

As long as you're investigating newbie-friendly editors, DrPython
(http://drpython.sourceforge.net/) is a good bet, too. Looks like it
will also allow you to convert line endings.

bubblboy

unread,
Mar 26, 2008, 10:34:36 PM3/26/08
to we...@googlegroups.com
Eric wrote:
> If you happen to have Cygwin available (you didn't mention that you
> did), the command-line tool dos2unix will do the trick. (Sometimes
> it's called d2u.) Or, if you're pushing your files around by FTP, look
> at your FTP program's options for converting/preserving line endings.

A little extra note: this is what is called "ASCII transfer mode" on
most FTP clients. Or try searching for "online dos2unix" on the web, who
knows ;). Oh, and if you do transform them to unix format before sending
them with FTP, make sure you transfer them in binary format!

b^4

Reply all
Reply to author
Forward
0 new messages