Wouter van Marle
wouter at squirrel-systems.com
Wed May 11 23:50:59 EDT 2005
On Wed, 2005-05-11 at 22:33 -0400, Jim Gallacher wrote: > Wouter van Marle wrote: > > On Wed, 2005-05-11 at 14:39 -0400, Jim Gallacher wrote: > > > >>Wouter van Marle wrote: > >> > >>>Hi all, > >>> > >>>I've a problem using the psp functions. > >>>What I am doing: > >>>I've a psp page, using some in-line python code, in between <% and > %> > >>>tags as per many examples. I however get all the time syntax errors > when > >>>running this through the psp.PSP.run() function. I'm sure my python > code > >>>as such is correct, so I'm at a loss here. Some code: > >>> > >>>page.psp: > >>><html><head> > >>><!-- rest of the header and so --> > >>></head> > >>><p> some text for the page </p> > >>><% > >>>for r in Results: > >>>%> > >>>I got the result <%=r%><br> > >>><% > >>># end indentation > >>>%> > >>> > >>>then in my python program I have made a list Results, containing > strings. > >>> > >>>page = psp.PSP(req, "page.psp") > >>>page.run({"Results": Results}) > >>> > >>>When running it like that, in my browser an error message appears > giving > >>>a syntax error, pointing at the "for" in the loop. > >>> > >>>Now from the documentation I can not find anything that would > forbid > >>>this. I haven't tried to set up a psp handler in apache, as I do > not > >>>intend to use this as such. I want to be able to pass a list of > results > >>>to the page, and then have the page fill itself, as smart template. > The > >>>main code should be out of the page. > >>> > >> > >>You found one of the warts in psp. Since your for loop contains no > >>further python statements, the parser gets confused. Try adding a > single > >>comment at the beginning of your for loop: > >> > >><p> some text for the page </p> > >><% > >>for r in Results: > >> # begin indentation - comment fixes the syntax error > >>%> > >>I got the result <%=r%><br> > >><% > >># end indentation > >>%> > > > > > > Still the same problem, Jim. > > I've also tried with comment before the 'for' statement, doesn't > make > > any difference :( > > > > Wouter. > > > > Actually, I was using the comment after the for statement to fix a > slightly different problem. I've tested the snippet for the psp page > you > posted before and it works for me. Perhaps you could post the actual > traceback? Mod_python error: "PythonHandler mod_python.publisher" Traceback (most recent call last): File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line 299, in HandlerDispatch result = object(req) File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", line 136, in handler result = util.apply_fs_data(object, req.form, req=req) File "/usr/lib/python2.3/site-packages/mod_python/util.py", line 361, in apply_fs_data return object(**args) File "/var/www/html/python/search.py", line 75, in search page = psp.PSP(req, templatefilename) File "/usr/lib/python2.3/site-packages/mod_python/psp.py", line 111, in __init__ self.load_from_file() File "/usr/lib/python2.3/site-packages/mod_python/psp.py", line 177, in load_from_file code = compile(source, filename, "exec") File "/var/www/html/templates/en/results.psp", line 20 for r in SongResults: ^ SyntaxError: invalid syntax And then to be complete I'll also give the psp template: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <meta name="author" content="Wouter van Marle" > <meta name="generator" content="screem 0.10.2" > <meta name="keywords" content="" > <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" > <meta http-equiv="Content-Script-Type" content="text/javascript" > <meta http-equiv="Content-Style-Type" content="text/css" > <link rel="stylesheet" type="text/css" href="../../main.css" > </head> <body> <h1>Search results for <%=artist%> and <%=song%></h1> <br> <!-- --> <p>Found the following songs:<p> <% # now it's getting interesting: we have to display the info out of a list of tuples! for r in SongResults: # begin %> <p>Song name: <%=r[0]%>, artist: <%=r[1]%>. This song is from the album <%=r[3]%> <% # end (for) %> </body> </html> Mind: I run this out of the main python script, though psp.PSP and then page.run(). That may make a difference as well! I haven't tested it as .psp page, which won't work as that way I can not pass the search results to the page. SongResults is a variable passed when calling the page: page.run({"SongResults": SongResults}) SongResults is a list of tuples, each containing four items, built up previously. I've tested the content of that variable, and it is correct. Wouter.
|