6 messages in com.googlegroups.pylons-discussconfusion with FormBuild
FromSent OnAttachments
dykang26 Sep 2006 21:42 
James Gardner27 Sep 2006 06:02 
dykang27 Sep 2006 16:46 
Anil27 Sep 2006 16:52 
James Gardner28 Sep 2006 04:33 
Sergey Lipnevich29 Sep 2006 08:06 
Subject:confusion with FormBuild
From:dykang (dyk@gmail.com)
Date:09/26/2006 09:42:22 PM
List:com.googlegroups.pylons-discuss

Hi,

I recently started playing with pylons and formbuild, and I went through the pylons formbuild integration and tried to use the formbuild.handle function.

as per the "tutorial", I created my function in my controller as the following:

def email_form_submit(self): results, errors, response = formbuild.handle( schema=schema.EmailFormSchema(), template='email_form.myt', form=build.MainForm )

if response: return response return Response( 'You are %s years old and have the following email: %s'%( results['age'], results['email'], ) )

This seemed to work at first, but I later discovered a very strange behavior. I opened a browser to hit http://<host>/controller/email_form_submit and the form was blank as expected. Then I filled out the form and hit submit. Everything seemed fine. Then my friend went to the same url (with firefox and safari on a different computer), and the form was prefilled with the values that I had typed in on my computer!

I'm using the following on my system:

./FormEncode-0.5.1-py2.4.egg ./FormBuild-0.1.5b-py2.4.egg ./Pylons-0.9.2-py2.4.egg ./Myghty-1.1-py2.4.egg ./MyghtyUtils-0.52-py2.4.egg

I'm new to python, pylons, and formbuild, so my tracings may be flawed, but I was at such a loss that I attempted to manipulate the formbuild.handle function to print out some debugging data.

Here is my modified version (the only 4 lines I added are the ugly print statements):

def handle(schema, template, form=None, data={}, params=None, context=None, render_response=None, fragment=False): print '1: params: %s' % str(params) print '1.5: data: %s' % str(data) if not isinstance(data, dict): values = {} for k in data.c.keys(): values[k] = getattr(data, k) data = values print '2: data: %s' % str(data) import formencode if params == None: from pylons import request params = {} for k in request.params.keys(): v = request.params.getall(k) if len(v) == 1: params[k] = v[0] else: params[k] = v

# raise Exception(params.getall('email')) if context == None: from pylons import c as context if not form: form = Form if not render_response: from pylons.templating import render_response c = context errors = {} data.update(params) print '3: data: %s' % str(data) results=data if len(params): try: results = schema.to_python(results, state=c) except formencode.Invalid, e: errors = e.error_dict or {} c.form = form(results, errors) if not len(params) or errors: return results, errors, render_response(template, fragment=fragment) return results, errors, '' from pylons import request params = {} for k in request.params.keys(): v = request.params.getall(k) if len(v) == 1: params[k] = v[0] else: params[k] = v

# raise Exception(params.getall('email')) if context == None: from pylons import c as context if not form: form = Form if not render_response: from pylons.templating import render_response c = context errors = {} _data.update(params) print '3: data: %s' % str(_data) results=_data if len(params): try: results = schema.to_python(results, state=c) except formencode.Invalid, e: errors = e.error_dict or {} c.form = form(results, errors) if not len(params) or errors: return results, errors, render_response(template, fragment=fragment) return results, errors, ''

As you can see from my code, in my controller I do not specify a value for the data or params parameters, so I expected I expected the output to look like this when I hit the page:

1: params: None 1.5: data: {} 2: data: {} 3: data: {}

and this when I submit:

1: params: None 1.5: data: {} 2: data: {} 3: data: {'go': '', 'age': '34', 'email': 'myem@email.org'}

Which, not surprisingly, it was when I first start paster and fill out the form and submit on my computer. However, if I don't restart paster, when my friend went to the form the output changed to this when viewing the 'empty' form:

1: params: None 1.5: data: {'go': '', 'age': '34', 'email': 'myem@email.org'} 2: data: {'go': '', 'age': '34', 'email': 'myem@email.org'} 3: data: {'go': '', 'age': '34', 'email': 'myem@email.org'}

So, I am at a total loss as to how the data parameter was filled out with data when I never passed anything in. I've "fixed" my problem by adding a data.clear() after the creation of the form, but I'd really like to understand how this behavior works. If anyone has any idea how this is happening, I'd really really really appreciate an explaination. Also, if anyone has any thoughts about a "real" fix, I'd appreciate that too.

Thanks, David