[Tutor] cgi.FieldStorage and dictionary.get(' ')
Patric Michael
patric at usa.net
Fri Dec 31 23:41:13 CET 2004
Forwarded to the list...
> On Thu, 30 Dec 2004 22:58:14 -0800, Patric Michael <patric at usa.net>
> wrote: > > First off, make sure that you have the "Content-Type:
> text/html\n\n" > line print before anything else whenever you send
> output to the web. > Presumably it worked for the form display, but
> you have to keep resending it > each time the page changes. > >
> Without seeing the def for login(failure), and not being familiar with
> SQL, I'll take > a pass on these lines, but I am curious why you
> declare failure as true or false in > the login function? > > Instead
> of form.get(), try this: > > print form['author'].value >
> print form['password'].value >
>
> Hi,
>
> def main(form):
> if form.has_key('author') and form.has_key('password'):
> author = form['author'].value
> password = form['password'].value
> q = Users.select(AND(Users.q.author==author,
> Users.q.password==password))
> if len(list(q))>0:
> chapters(author)
> else:
> login(failure=True)
> else:
> login(failure=False)
>
> Using form['author'].value works so long as it's assigned to a
> variable before being looked up by sqlobject. This is interesting
> because, if I try this from the interperter:
>
> >>> author = form['author'].value
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> AttributeError: 'str' object has no attribute 'value'
Right. If I recall correctly, the .value method is specific to the cgi
module since FieldStorage() is actually a bunch of dictionaries
contained in a list.
The .value method shortcuts the need to extract the dictionary from the
list, then extract the key/value pair from the result and lets you grab the
value directly. (Someone correct me if I misremember this, please!)
>
> I define login(failure) as True or False because the first time the
> user reaches the login page they shouldn't be presented with a failure
> message. The second time round they should.
Oh cool. I hadnt thought of that. I usually use a try: statement to test
for one of the keys to determine whether the script sends the initial login
form, or the login form with appropriate error messages prepended.
That way I can explicitly indicate which field(s) need attention.
>
> Everything happens through a driver script:
>
> #!/usr/local/bin/python
>
> print 'Content-type: text/html\n\n'
>
> import cgi
> from spanishlabs.gui import *
>
> if __name__ == '__main__':
> main(form=cgi.FieldStorage())
>
> Thanks for your help!
>
No problem!
Patric
More information about the Tutor
mailing list