Python and HTML hidden input variables
Thomas Wouters
thomas at xs4all.nl
Fri Oct 22 10:05:10 EDT 1999
On Fri, Oct 22, 1999 at 01:28:12PM +0000, rainer2207 at my-deja.com wrote:
> I was wondering how I could pass a variable using the hidden field. That
> is I'd like to do something like this:
> test = "Hello"
> print "<input type=\"HIDDEN\" name=\"username\" value=test>"
> but this returns the string test rather than "Hello". Is there anyway
> I can get it to pass "Hello"? Am I right in leaving out the double
> quotes for the value part? Any help would be appreciated. Thanks.
You can do it three ways:
>>> print '<input type="HIDDEN" name="username" value="', test, '">'
(Note that i mixed single and double quotes to avoid the escaping of the
originally used quotes... makes for better reading, imho.)
But this inserts a space between the quotes and the value of 'test',
>>> print '<input type="HIDDEN" name="username" value="' + test + '">'
This doesn't insert a space, but might not work correctly if 'test' is not a
string (but, for instance, an object doing something entirely different on
'+'.)
print '<input type="HIDDEN" name="username" value="%s">'%test
This uses C's 'printf'-style string creation... the '%s' is replaced by the
(string-)value of 'test'. If 'test' isn't a string, it's converted into one
(or an error is raised, if it refuses.)
Note that the ""%... string creation thing can be tricky when you want to
include more things. You need to make the argument to % a tuple, like this:
print '<input type="HIDDEN" name="%s" value="%s"'%(test1, test2)
Please do see the tutorial for more info about print tricks (and about all
other neat tricks in python, of course ;)
http://www.python.org/doc/tut/-ly y'rs,
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list