Python and HTML hidden input variables

D'Arcy J.M. Cain darcy at vex.net
Sat Oct 23 05:42:58 EDT 1999


rainer2207 at my-deja.com wrote:
> I have a Python script which displays a form and has a hidden input. The
> relevant code part looks something like this:

> print "<form method=\"post\" action=\"sendmsg.cgi\">"
> print "<textarea name=\"sndmsg\" rows=\"7\" cols=\"73\" nowrap>"
> print "</textarea>"
> print "<input type=\"HIDDEN\" name=\"username\"  value="test">"
> print "<br>"
> print "<input type=\"submit\" value=\"Talk\">"
> print "</form>"

Use the features of the language.  Python has a triple quote that can
make your programs look a lot cleaner.  This is especially nice if you
do a lot of CGI programming.  Here is how I would write the above (sort
of - I put all my keywords in caps)

print """<form method="post" action="sendmsg.cgi">
    <textarea name="sndmsg" rows="7" cols="73" nowrap>
    </textarea>
    <input type="HIDDEN" name="username"  value="test">
    <br>
    <input type="submit" value="Talk">
    </form>"""

> test = "Hello"
> print "<input type=\"HIDDEN\" name=\"username\"  value=test>"

Others have pointed out the '%' operator.  It also works with triple
quoted strings.  Here's the above with your variable string.

print """<form method="post" action="sendmsg.cgi">
    <textarea name="sndmsg" rows="7" cols="73" nowrap>
    </textarea>
    <input type="HIDDEN" name="username"  value="%s">
    <br>
    <input type="submit" value="Talk">
    </form>""" % test

-- 
D'Arcy J.M. Cain <darcy at caingang.com>      |  Democracy is three wolves
http://www.druid.net/darcy/                |  and a sheep voting on         
+1 416 425 1212     (DoD#0082)    (eNTP)   |  what's for dinner.




More information about the Python-list mailing list