"wizard" style forms in web2py

339 views
Skip to first unread message

Robert O'Connor

unread,
May 28, 2010, 7:20:00 PM5/28/10
to web2py-users
Hey,

I need to implement a "wizard" style form. (things will exist on
different screens; each "step" or page will post to the next page and
have those values stored and used in the next step.

Now here's the problem: there's seems to be very few examples in
developing something like this in web2py... Does anybody know either
of an app that does this that I can look at for examples or perhaps a
strategy of implementing this?

If you're in the United States -- Happy Memorial Day weekend!
-Robert O'Connor

mdipierro

unread,
May 29, 2010, 11:19:30 AM5/29/10
to web2py-users
db.define_table('mytable',
Field('field1'), Field('field2'), Field('field3'),
Field('field4'))

make sure they all have defaults.

def wizard():
fields=[['field1'], # first page
['field2','field3'], # second page
['field4']] # third page
record_id =int(request.args(0) or 0)
page=int(request.args(1) or 0)
form = SQLFORM(db.mytable,record_id,fields=fields[page])
if form.accepts(request.vars,session):
if page<len(fields):
redirect(r=request,args=(form.vars.id,page+1))
else: redirect(r=request,f='form_completed'))
return dict(form=form, page=page)

I did not try it. May need some debugging.

Mathieu Clabaut

unread,
May 29, 2010, 11:39:32 AM5/29/10
to web...@googlegroups.com
Nice idea !
I needed something similar... I'll give a try..
Thanks !

Thadeus Burgess

unread,
May 29, 2010, 11:57:02 AM5/29/10
to web...@googlegroups.com
I would use the jQuery form wizard plugin. It is very nice and dose
the pagination for you.

The only issue is if the data in the second step depend on what is in
the first step, then you would need to use some ajax/javascript to
alter accordingly.

--
Thadeus

Iceberg

unread,
May 29, 2010, 12:29:38 PM5/29/10
to web2py-users
It is always fun to see Massimo asking questions, because he can
usually comes up with a clean solution based on some unpopular web2py
trick. :) This time, it is the SQLFORM(..., fields=...) that I did
not notice. Thanks. :)

Thadeus also gives a good suggestion. And this kind of logic is better
implemented by a client side technic to gain faster UI response.

--
Iceberg


On May29, 11:57pm, Thadeus Burgess <thade...@thadeusb.com> wrote:
> I would use the jQuery form wizard plugin. It is very nice and dose
> the pagination for you.
>
> The only issue is if the data in the second step depend on what is in
> the first step, then you would need to use some ajax/javascript to
> alter accordingly.
>
> --
> Thadeus
>
> On Sat, May 29, 2010 at 10:39 AM, Mathieu Clabaut
>
>
>
> <mathieu.clab...@gmail.com> wrote:
> > Nice idea !
> > I needed something similar... I'll give a try..
> > Thanks !
>

Robby O'Connor

unread,
May 29, 2010, 8:36:57 PM5/29/10
to web...@googlegroups.com
Okay,

Looking at this deeper, I there will be a complete form (one covering
one db table; then another covering a completely different database
table. Let me see if I got this right.

Can this implementation idea work for that case?

--rob

mdipierro

unread,
May 29, 2010, 9:37:16 PM5/29/10
to web2py-users
Yes. The idea is to user request.args(0) to decide the page of the
wizard and the relative form.

Robby O'Connor

unread,
May 29, 2010, 9:50:49 PM5/29/10
to web...@googlegroups.com
Okay thank you! P.S. I'm working with Sahana Py (now referred to as
Sahana Eden)

=)
--rob

Message has been deleted

Richard Vézina

unread,
Aug 28, 2012, 10:00:28 AM8/28/12
to web...@googlegroups.com
Tuples are unmutable (you can't change them), so they don't have a replace attribute. I you want to change your the values into your tuples use list instead.

Richard

On Tue, Aug 28, 2012 at 3:44 AM, Bill Thayer <bill....@live.com> wrote:
The example from the web2py application cookbook runs fine but when I tweak it I get

<type 'exceptions.AttributeError'> 'tuple' object has no attribute 'replace'


In the code below, mine is on top named test() for input a test request. The function named wizard came from the cookbook.
def tests():
    STEPS
= {
         
0: ('db.test_order.charge_number', 'db.test_order.program', 'db.test_order.requestor','db.test_order.need_date', 'db.test_order.comments'), #collect order details first
         
1: ('db.test.part', 'db.test.sample', 'db.test.test_type', 'db.test.process'), # feilds for 1st page
         
2: ('db.test.cal_standard', 'db.test.start', 'db.test.stop', 'db.test.step'), # fileds for 2nd page
         
3: ('db.test.vds', 'db.test.ids'), # fields for 3rd page
         
4: URL('done')} # url when wizard completed
         
    step
= int(request.args(0) or 0)
   
if not step in STEPS: redirect(URL(args=0))
   
   
    fields
= STEPS[step]
   
print "Fields: " + str(fields) + " Step " + str(step)
   
if step==0:
    session
.wizard = {}
    mytable
= db.test_order
   
else:
        mytable
= db.test
       
   
if isinstance(fields, tuple):
    form
= SQLFORM.factory(*[f for f in mytable if f.name in fields])
         
       
if form.accepts(request, session):
        session
.wizard.update(form.vars)
        redirect
(URL(args=step+1))
       
else:
        mytable
.insert(**session.wizard)
        session
.flash = T('wizard completed')
        redirect
(fields)
   
       
return dict(form=form, step=step)

def wizard():
    STEPS
= {0: ('field1', 'field2'), # fields for 1st page
             
1: ('field3', 'field4'), # fields for 2nd page
             
2: ('field5', 'field6'), # fields for 2nd page
             
3: URL('done')} # url when wizard completed
    step
= int(request.args(0) or 0)
   
if not step in STEPS: redirect(URL(args=0))
    fields
= STEPS[step]
   
print "Fields: " + str(fields) + " Step " + str(step)
   
if step==0:
        session
.wizard = {}
   
if isinstance(fields,tuple):
        form
= SQLFORM.factory(*[f for f in db.mytable if f.name in fields])
       
if form.accepts(request,session):
            session
.wizard.update(form.vars)
            redirect
(URL(args=step+1))            
   
else:
        db
.mytable.insert(**session.wizard)
        session
.flash = T('wizard completed')
        redirect
(fields)
   
return dict(form=form,step=step)

   

def done():
   
return dict(message="End of wizard", back=A("New wizard", _href=URL("wizard")))                  


--
 
 
 

Reply all
Reply to author
Forward
0 new messages