about FORM and db.update

8 views
Skip to first unread message

Sebastian E. Ovide

unread,
May 6, 2009, 1:20:49 PM5/6/09
to web...@googlegroups.com
Hi All,

in an action that updates a FORM , when the form accepts the new values, it updates the DB correctly, but when redraw the page it display the old values. If I press Submit again (keeping the old values in the form) it displays the new values entered previously...  and so on for ever....

the work around was to add a redirect to the same controller just after the db.update.

Is it the correct way to deal with custom FORMS ? Am I missing something ?

thanks

def test():
    form = None
    # find tests profile of current user
    query=db.tests.owner_id==auth.user.id
    tests = db(query).select()
    if len(tests)>0:
        #get the first found
        test=tests[0]
                    
        form=FORM(TABLE(TR("Name:",INPUT(_type="text",_name="name",value=test.name,requires=IS_NOT_EMPTY())),                       
                        TR("",INPUT(_type="submit",_value="SUBMIT"))
                    ))             
       
        if form.accepts(request.vars,session):
            db(query).update(name=form.vars.name)
            # HERE THE REDIRECT !!!!!!
            redirect(URL(r=request,f="test"))           
   
    return dict(form=form)

----------------------------
Sebastian E. Ovide


mdipierro

unread,
May 6, 2009, 1:52:13 PM5/6/09
to web2py Web Framework
You can do

if form.accepts(request.vars,session,keepvalues=True)

You can also simplify your code

def test():
tests = db(db.tests.owner_id==auth.user.id).select()
if tests:
form=SQLFORM(db.tests,tests[0],fields=['name'])
if form.accepts(request.vars,session):
# session.flash = '....'
redirect(URL(r=request,f="test"))
else:
form=None
return dict(form=form)



On May 6, 12:20 pm, "Sebastian E. Ovide" <sebastianov...@gmail.com>
wrote:

Sebastian E. Ovide

unread,
May 6, 2009, 2:08:18 PM5/6/09
to web...@googlegroups.com
Thanks Massimo !

I've tried to use SQLFORM before but it displays also the ID even if I do not include it in the fields list. Furthermore I need to change some fields defaults such as TEXTAREA size etc... (in my app there are more than 1 field)


----------------------------
Sebastian E. Ovide

mdipierro

unread,
May 6, 2009, 2:41:22 PM5/6/09
to web2py Web Framework
SQLFORM(...,showid=False)

On May 6, 1:08 pm, "Sebastian E. Ovide" <sebastianov...@gmail.com>
wrote:
> Thanks Massimo !
>
> I've tried to use SQLFORM before but it displays also the ID even if I do
> not include it in the fields list. Furthermore I need to change some fields
> defaults such as TEXTAREA size etc... (in my app there are more than 1
> field)
>
> ----------------------------
> Sebastian E. Ovide
>
Reply all
Reply to author
Forward
0 new messages