Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11672

Re: CGI: Assign FieldStorage values to variables

References <92d066ff-d0ee-432f-b512-1f2fa01ba681@a12g2000yqi.googlegroups.com> <bd2e169b-6bcb-4578-ac49-5fba344aa150@y16g2000yqh.googlegroups.com>
Date 2011-08-17 10:41 +0100
Subject Re: CGI: Assign FieldStorage values to variables
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.122.1313574104.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Aug 17, 2011 at 10:19 AM, Gnarlodious <gnarlodious@gmail.com> wrote:
> import cgi, os
> os.environ["QUERY_STRING"] = "name1=Val1&name2=Val2&name3=Val3"
> form=cgi.FieldStorage()
>
> form
>
> dict = {}
> for key in form.keys(): dict[ key ] = form[ key ].value
>

You could probably use a list comp for this, but there's not a lot of
difference. (By the way, you should be aware that you're shadowing the
builtin 'dict' here. That's not a problem, but be aware.)

dict.update(((key,form[key].value) for key in form))

That's a generator that returns a (key,value) tuple for each form
element, which update() will happily use.

But the last line:
> locals().update(dict)
is, IMHO, a very bad idea. Rename your dictionary to 'request' or
'data' or 'form' or something, and then reference form['name3']
instead; or, be explicit:
name3=form['name3']

Incidentally, you seem to be working at the module level, where
locals() is globals() (and I mean that literally - 'locals() is
globals()' is True). You may not be able to do this with locals() in a
function:
http://docs.python.org/library/functions.html#locals

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

CGI: Assign FieldStorage values to variables Gnarlodious <gnarlodious@gmail.com> - 2011-08-17 02:06 -0700
  Re: CGI: Assign FieldStorage values to variables Gnarlodious <gnarlodious@gmail.com> - 2011-08-17 02:19 -0700
    Re: CGI: Assign FieldStorage values to variables Chris Angelico <rosuav@gmail.com> - 2011-08-17 10:41 +0100
    Re: CGI: Assign FieldStorage values to variables Chris Rebert <clp2@rebertia.com> - 2011-08-17 09:23 -0700
  Re: CGI: Assign FieldStorage values to variables Chris Angelico <rosuav@gmail.com> - 2011-08-17 10:25 +0100
    Re: CGI: Assign FieldStorage values to variables Gnarlodious <gnarlodious@gmail.com> - 2011-08-17 20:52 -0700
  Re: CGI: Assign FieldStorage values to variables Gnarlodious <gnarlodious@gmail.com> - 2011-08-17 02:20 -0700
  Re: CGI: Assign FieldStorage values to variables Nobody <nobody@nowhere.com> - 2011-08-17 13:45 +0100

csiph-web