strange problem with FORMS

14 views
Skip to first unread message

Jim C

unread,
Mar 18, 2009, 1:43:20 AM3/18/09
to web2py Web Framework
I'm just learning web2py and I've got a simple example whose behavior
I don't understand. I've got a page with a form on top that sets a
session variable holding the default year.
On the bottom of the page I've got an SQLTABLE where the records
selected should be from the
selected year. What I'm observing is that the form on top accepts
the year value and the bottom of the page updates but the form on top
always seems to be one update behind.

In the controller:

def admin_test():

# form1 sets the default year to work on
if not session.bene_year:
session.bene_year = datetime.datetime.now().year

form1 = FORM('Set Year (current value is '+session.bene_year+') :
',
INPUT(_name='year', _value=session.bene_year,
requires=IS_NOT_EMPTY()),
INPUT(_type='submit'))

# set the default year from the form
if FORM.accepts(form1, request.post_vars, session,
formname='form_one'):
session.bene_year = request.post_vars.year
# set the year
response.flash = 'The default year is set to
'+session.bene_year

# fetch the current beneficiaries for the default year
dbrecords = db(db.beneficiary.year==session.bene_year).select
(db.beneficiary.ALL)
records = SQLTABLE(dbrecords, truncate='1024')

return dict(form1=form1, dbrecords=dbrecords, records=records)

The sequence I'm observing is:

1) Initial page view, year is set to 2009. All OK.
2) Enter 2007 and press 'Submit Query'. Page refreshes, db records
from 2007 appear below, and
form one reads: "Set Year (current value is 2009) : [ 2009 ]".
Not OK.
3) Replace 2009 with 2005 and press 'Submit Query'. Page refreshes, db
records from 2005 appear below and the form now reads: "Set Year
(current value is 2007): [ 2007 ]".
4) etc.

Thanks!

mdipierro

unread,
Mar 18, 2009, 2:21:52 AM3/18/09
to web2py Web Framework
Are you using python 2.6 or 2.5?

Massimo

Jim C

unread,
Mar 18, 2009, 3:12:57 AM3/18/09
to web2py Web Framework
Hi Massimo,

I'm using 2.5.1 that came with the Mac OS X distribution: web2py
Version 1.58 (2009-03-10 10:51:30)

--Jim

Yarko Tymciurak

unread,
Mar 18, 2009, 4:01:13 AM3/18/09
to web...@googlegroups.com
do you have the source distribution?

or are you running web2py.app?

Jim C

unread,
Mar 18, 2009, 10:48:29 AM3/18/09
to web2py Web Framework
I'm running web2py.app

On Mar 18, 1:01 am, Yarko Tymciurak <yark...@gmail.com> wrote:
> do you have the source distribution?
> or are you running web2py.app?
>

Jim C

unread,
Mar 18, 2009, 12:16:27 PM3/18/09
to web2py Web Framework
just a small unrelated item, the line:

session.bene_year = datetime.datetime.now().year

should be

session.bene_year = str(datetime.datetime.now().year)

otherwise you'll get a type mismatch the first time you run this.

Yarko Tymciurak

unread,
Mar 18, 2009, 1:55:07 PM3/18/09
to web...@googlegroups.com
On Wed, Mar 18, 2009 at 9:48 AM, Jim C <jgc9...@gmail.com> wrote:

I'm running web2py.app

I think this explains it then.

For development, I think you want to run the source version.

You can try if shell works from a command line (it should):

    web2py.app -S my_app -M

instead of 

    python web2py,.py -S my_app -M

Python will try to find web2py.py --- which doesn't exist with web2py.app;  this is why you get nothing with dict.

Let us know if this helps.

- Yarko 

mdipierro

unread,
Mar 18, 2009, 2:10:44 PM3/18/09
to web2py Web Framework
I cannot reproduce this. Can you try

session.bene_year = form.post_vars.year

instead of

session.bene_year = request.post_vars.year

and see if it makes any difference?

On Mar 18, 12:43 am, Jim C <jgc94...@gmail.com> wrote:

Jim C

unread,
Mar 18, 2009, 2:42:34 PM3/18/09
to web2py Web Framework

Thanks, neither suggestion has helped.

Yarko, I double click on the app to start it. On a Mac an app is just
a directory, and a shell doesn't understand
the specialness of the .app suffix, only the Finder does.

Massimo, the response.flash is always correct, just the label and the
default value of the text field is out of sync.

If I change
session.bene_year = request.post_vars.year
to
session.bene_year = form.post_vars.year
I get a runtime error
AttributeError: 'FORM' object has no attribute 'post_vars'

===

Here's an even simpler version of the bug:

model:

db.define_table('beneficiary',
SQLField('name','string'),
SQLField('year','integer'))


default controller:

def index():

import datetime

# form sets the default year to work on
if not session.bene_year:
session.bene_year = str(datetime.datetime.now().year)

form = FORM('Set Year (current value is '+session.bene_year+') :
',
INPUT(_name='year', _value=session.bene_year,
requires=IS_NOT_EMPTY()),
INPUT(_type='submit'))

# set the default year from the form
if FORM.accepts(form, request.post_vars, session,
formname='form_one'):
session.bene_year = request.post_vars.year
# set the year
response.flash = 'The default year is set to
'+session.bene_year

# fetch the current beneficiaries for the default year
records = SQLTABLE(db
(db.beneficiary.year==session.bene_year).select(db.beneficiary.ALL),
truncate='1024')

return dict(form=form, records=records)

Jim C

unread,
Mar 19, 2009, 10:41:28 PM3/19/09
to web2py Web Framework

I've found a solution. Thanks Yarko, for your suggestion.
I downloaded the source and used the shell interface
to explore the FORM object.

The new working code is:

def index():

import datetime

# form sets the default year to work on
if not session.bene_year:
session.bene_year = str(datetime.datetime.now().year)

form = FORM('Set Year: ',
INPUT(_name='year', _value=session.bene_year,
requires=IS_NOT_EMPTY()),
INPUT(_type='submit'))

# set the default year from the form
if FORM.accepts(form, request.post_vars, session,
formname='form_one'):
session.bene_year = request.post_vars.year
form.components[1].attributes['_value']=session.bene_year #
reset the default value of the INPUT field
# set the year
response.flash = 'The default year is now '+session.bene_year

Yarko Tymciurak

unread,
Mar 19, 2009, 10:58:16 PM3/19/09
to web...@googlegroups.com
Glad to hear!  Thanks for letting us know.
Reply all
Reply to author
Forward
0 new messages