A couple of Python CGI questions

Andrew Clover and-google at doxdesk.com
Tue Mar 30 13:30:01 EST 2004


"sean" <sean_berry at cox.net> wrote:

> userid = form.getfirst("userid")

Yes, this the the most convenient way of using FieldStorage, in Python 2.2 upwards.

> can I collect post variables in the same fashion?

Yes. In a POST request, name/value pairs are read from the body; any
pairs in a query string are ignored.

> Is there any way to access variables from within a string like perl cgi?

Sort of. The basic substitution mechanism is:

  '<el> Hello %s %s! </el>' % (cgi.escape(title), cgi.escape(person.name))

Or you can use named substitutions by using a dictionary:

  '<el> Hello %(title)s %(name)s! </el>' % {
    'title': cgi.escape(title), 'name ': cgi.escape(name)
  }

And you can get a dictionary containing local variables using locals():

  t= cgi.escape(title)
  n= cgi.escape(name)
  '<el> Hello %(t)s %(n)s! </el>' % locals()

This is as far as Python goes, but there are of course a million templating
languages that take things up from there and which are a sensible choice
for anything more complicated. [insert standard PXTL plug here. ed.]

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/



More information about the Python-list mailing list