[mod_python] for loop doubt

Graham Dumpleton grahamd at dscpl.com.au
Mon Jan 2 16:12:58 EST 2006


Using req.write() in PSP partly defeats the purpose of using it in the
first place. Ie., to avoid having to fiddle with req object directly. For
another example of how to do for loops see:

<html>
  <head>
    <title>
      3-2.py
    </title>
  </head>
  <body>
    <table >
<%
for key in req.subprocess_env.keys():
      # indent
%>
      <tr>
        <td><%=key%></td>
        <td><%=req.subprocess_env[key]%></td>
      </tr>
<%
# end for
%>
    </table>
    <p>test</p>
  </body>
</html>


Indenting levels is critical with PSP. It defaults internally to using a tab
width for laying out its own auto generated code. If you use a different
indent level you have to be quite careful to include the comment hints
so it knows what indent level you are using, eg., "# indent" in the above.

See:

  http://www.modpython.org/pipermail/mod_python/2005-May/018102.html

for further information about indenting issues and further loop examples.

Graham



Mike Looijmans wrote ..
> There are several errors, so I'm not surprised that you cannot find it:
> 
> - "res" is probably a misspelled "req"
> - Indentation is completely wrong
> - ending %> after for is probably not what you meant.
> - using req.write(...) to output a PSP script?
> - invalid colon at end of that line
> 
> You probably meant the following:
> ....
> 	<select name="dob">
> 	<%
> 	for n in range(31):
> 		req.write("<option>%s</option>" % n)
> 	# end for
> 	%>
> 	</select>
> ....
> 
> This cryptic thing should do the same, but quicker and without indentation:
> 
> <%= [("<option>%s</option>" % n) for n in range(31)] %>
> 
> --
> Mike Looijmans
> Philips Natlab / Topic Automation
> 
> 
> python eager wrote:
> > Hi
> >    in python server pages i am using for loop . but it will not work
> > properly. The code will be as follow as :
> > <td> Date of Birth </td>
> >                      <td>
> >                       
> >                             <select name="dob">
> >                                         <% for n in range(31):   %>
> >                                               res.write("<option>" +
> > <%=n%> + "</option>"):
> >                                        
> >                             </select>                    
> >                                     nbsp;       
> >                     </td>
> >  
> > But it will not work. What is the error , i cannt found the error. Would
> > any one have solution please send mail me. Thank You Very much. and also
> > Happy New Year
> >  
> > Note : very urget for me .reply wanted
> >  
> > regards
> > Python_Eager
> >  
> > 
> > ------------------------------------------------------------------------
> > Yahoo! for Good - Make a difference this year. 
> > <http://us.rd.yahoo.com/mail_us/taglines/charity/*http://brand.yahoo.com/cybergivingweek2005/>
> > 
> > 
> > 
> > ------------------------------------------------------------------------
> > 
> > _______________________________________________
> > Mod_python mailing list
> > Mod_python at modpython.org
> > http://mailman.modpython.org/mailman/listinfo/mod_python
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python


More information about the Mod_python mailing list