[Tutor] cgi FieldStorage __str__ problem - I think...

Roman Suzi rnd at onego.ru
Wed Jun 20 07:43:47 EDT 2001


On Wed, 20 Jun 2001, Simon Brunning wrote:

> I'm having a bit of a problem with my first CGI script. The idea is to end
> an email based on a form.
> 
> I set up a template for the email as follows:
> 
> mailtemplate = '''From: %(email)s
> Subject: %(subject)s
> Email from %(name)s
> %(comments)s'''
> 
> Then I substitute the data from the form into the template as follows:
> 
> form = cgi.FieldStorage()
> mailtext = mailtemplate % form

This is because instead of 
form["email"] 

you need

form["email"].value

I do not know why it was made in module cgi, but it works this way.

I am using this:

def addnames(namespace_dict,NAMES=[],DEFAULT=None,SANITIZE=str,**defaults):
  import cgi
  form = cgi.FieldStorage()
  dict = {}
  for i in NAMES+defaults.keys():
    if form.has_key(i):
      fval = SANITIZE(form[i].value)
    else:
      if defaults.has_key(i):
        fval = defaults[i]
      else:
        fval = DEFAULT
    dict[i] = fval
  namespace_dict.update(dict)

To add names to dict.
(Usually, I add them stright to globals())

addnames(
      globals(),
      ["email","name"],
      "",
      str,   # you can use some other function to check input
      email="defaul at email",
      name="Mr. Default"
)

 
> Trouble is, the mailtext field ends up looking like this:
> 
> From: MiniFieldStorage('email', 'a at b.c')
> Subject: MiniFieldStorage('subject', 'Spam')
> Email from MiniFieldStorage('name', 'Fred')
> MiniFieldStorage('comments', 'Egg and chips...')
> 
> Looks to me like the FieldStorage object's values are instances of some
> MiniFieldStorage class, and the __str__ method of that class is just
> returning the __repr__. Is this correct?
> 
> If so, what do I do about it?
> 
> Cheers,
> Simon Brunning
> TriSystems Ltd.
> sbrunning at trisystems.co.uk
> 
> 
> 
> 
> 
> -----------------------------------------------------------------------
> The information in this email is confidential and may be legally privileged.
> It is intended solely for the addressee. Access to this email by anyone else
> is unauthorised. If you are not the intended recipient, any disclosure,
> copying, distribution, or any action taken or omitted to be taken in
> reliance on it, is prohibited and may be unlawful. TriSystems Ltd. cannot
> accept liability for statements made which are clearly the senders own.
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 

Sincerely yours, Roman A.Suzi
-- 
 - Petrozavodsk - Karelia - Russia - mailto:rnd at onego.ru -
 





More information about the Python-list mailing list