Why is CGI module so complicated?

Magnus L. Hetland mlh at idt.ntnu.no
Thu Aug 12 10:02:59 EDT 1999


Ian Clarke <I.Clarke at strs.co.uk> writes:

> > : Can anyone explain what the point of the FieldStorage and
> > : MiniFieldStorage objects are?

> How about go with
> the method I described in my first post (where cgi fields are returned
> as the types that they actually are, rather than being encased in
> MiniFieldStorage objects).  Were more than one item is selected for a
> given field a list could be returned, and for a file upload a tuple
> could be returned containing the filename and the contents of the file -
> or if that is not considered extendable enough, a CGIFileUpload object
> could be returned containing the necessary fields.

Or just a File object... And then we could add a "name" attribute or
something to them... (Which would be empty in cases where the object
is not taken from a standard named file)... :)

> This would mean that for the vast majority of CGI fields which are just
> normal text, retrieval is very straight-forward and intuitive, just like
> accessing strings stored in a dictionary.  More complexity is only
> introduced when the user wants to do more complicated stuff (and even
> then, it is pretty simple).  As mentioned in my previous post, it would
> also allow the fields to be used as the substitutions for a
> TemplateDocument directly, which can be quite a useful trick.

Or - you could rewrite TemplateDocument to work with the CGI
classes... That would just be a matter of adding some code to
recognize whether it was a standard dictionary or not...

> If I get some spare time today I will create a small module which will
> provide this interface to CGI (although if anyone else wants to do it,
> feel free!).  It would obviously be better if (assuming I can convince
> others that this is a better way) this was integrated into the built-in
> cgi module.

Well... I suspect it can be *very* small for simple applications (that
don't use file upload, the methods values() or items())...

----- simplecgi.py -----

import cgi, UserDict

class FieldStorage(userdict.UserDict):
    def __init__(self):
        self.data = cgi.FieldStorage()
    def __getitem__(self, key): return self.data[key].value
        
------------------------

Or you *could* use the old classes, like FormContentDict that are
already defined (but deprecated).

> 
> Ian.

--

  Magnus              Making no sound / Yet smouldering with passion
  Lie          The firefly is still sadder / Than the moaning insect
  Hetland                                       : Minamoto Shigeyuki




More information about the Python-list mailing list