Trouble with components

72 views
Skip to first unread message

G

unread,
Aug 3, 2011, 11:46:09 PM8/3/11
to web2py-users
Hello,
I am trying to make a simple component which has a form that contains
just buttons. It seems to work OK when I use LOAD(... ajax=False) and
have only one component on my page. However, I'd like to have multiple
components on one page. When I tried that, still with ajax=False,
everything looks OK, but the form doesn't do anything (that is, the
action that's supposed to happen when a button is pushed never
happens). Similarly, if I set ajax=True, the same problem occurs (no
action).
I then tried setting ajax_trap = True after which when I click a
button in the component, the component disappears and the text
"invalid request" appears. I found this very difficult to debug, but
finally by putting a print statement in the rewrites.py file I found
that the path that was trying to be accessed looks as follows, from
which it's why it is an invalid request:

/devel/default/(<gluon.html.XML object at 0x14721510>,)

What is the correct way to have multiple components with simple
customized forms on a single page?

The simple component I am using has the following function which uses
the generic.load:

def inits():
obs = _getObs()
form = FORM(TABLE(TR(TD(TAG.BUTTON("Reset
Receiver",_type="submit",_name="initRSS", _value="yes")),
TD(TAG.BUTTON("Reset DSP",_type="submit",_name="initDSP",
_value="yes")),
TD(TAG.BUTTON("Auto Level",_type="submit",_name="autoLevel",
_value="yes")),
TD(TAG.BUTTON("Auto Equalize",_type="submit",_name="autoEQ",
_value="yes")))))
if form.accepts(request.vars,session):
if request.vars.initRSS == 'yes':
print "init RSS"
obs.simpleRSSSetup()
if request.vars.initDSP == 'yes':
print "init DSP"
obs.simpleDSPSetup()
if request.vars.autoLevel == 'yes':
print "autoLevel"
obs.autoLevel()
if request.vars.autoEQ == 'yes':
print "autoEQ"
obs.autoEQ()
return dict(form=form)

I have a second similar component. I include them both in the larger
page as:
{{=LOAD('default','inits.load')}}
{{=LOAD('default','posControl.load')}}

Thank you for any suggestions on how to make this work.
G

pbreit

unread,
Aug 4, 2011, 12:13:14 AM8/4/11
to web...@googlegroups.com
I think with ajax=False you're going to run into problems because the end result is both of the forms on one page of HTML and the controller might get confused on which to handle.

But I would think ajax=True should work. ajax_trap should be unnecessary.

Can you get one form to work by itself with ajax=True?

G

unread,
Aug 4, 2011, 12:29:24 AM8/4/11
to web2py-users
I just tried a single component with ajax=True and found that it was
not responding.
Digging more deeply, I am finding that request.vars includes only
_formname="default" and _formkey=<long hex number>. Only with a single
form and ajax=False do I see the expected autoEQ = "yes".

Any idea what's going on?
Thanks,
G

Anthony

unread,
Aug 4, 2011, 1:43:48 AM8/4/11
to web...@googlegroups.com
Forms in ajax components and non-ajax components with ajax_trap=True are submitted via ajax, and the form variables are serialized via the jQuery .serialize() method. As explained here (http://api.jquery.com/serialize/), the .serialize() method doesn't know what triggered the form submission, so it cannot include the values of any submit buttons. For now, here is one possible workaround using a hidden field: http://forum.jquery.com/topic/submit-event-serialize-and-submit-buttons-get-the-button-name. Perhaps we can come up with a more general solution.
 
Anthony

G

unread,
Aug 4, 2011, 2:12:56 AM8/4/11
to web2py-users
Hmm, so it sounds like components with multiple buttons are basically
not supported easily. I am writing an application to control a bunch
of scientific equipment. For each piece of equipment I was planning to
make one component to monitor and control it, and then put all these
components on one web page. Most of the controls make sense to
implement as buttons.
Does anyone have a suggestion of a better way to organize such an
application to play better with web2py etc.?
Thanks again,
G

On Aug 3, 10:43 pm, Anthony <abasta...@gmail.com> wrote:
> Forms in ajax components and non-ajax components with ajax_trap=True are
> submitted via ajax, and the form variables are serialized via the jQuery
> .serialize() method. As explained here (http://api.jquery.com/serialize/),
> the .serialize() method doesn't know what triggered the form submission, so
> it cannot include the values of any submit buttons. For now, here is one
> possible workaround using a hidden field:http://forum.jquery.com/topic/submit-event-serialize-and-submit-butto....

pbreit

unread,
Aug 4, 2011, 5:23:23 AM8/4/11
to web...@googlegroups.com

G

unread,
Aug 4, 2011, 7:33:17 PM8/4/11
to web2py-users
I think I found a workable (but slightly annoying) solution: create
several mini-forms that only have the submit button, each with a
different form name. Then I can just use many
if form_blah.accepts(request.vars, session, formname='form_blah'):
clauses. I'd still be interested in knowing if there is a better way.
Another idea I toyed with was using jQuery to connect the .click
action of a button to cause a jQuery.post(URL('myfunction'),
{button_name : "yes"}) to send the button press to myfunction, but I'm
new to jQuery, so didn't put much time into trying it and don't know
if it would work.
Thanks,
G

On Aug 4, 2:23 am, pbreit <pbreitenb...@gmail.com> wrote:
> Coupla other approaches:http://www.johnnycode.com/blog/2010/04/08/jquery-form-serialize-doesn...http://stackoverflow.com/questions/4007942/jquery-serializearray-does...

Massimo Di Pierro

unread,
Aug 4, 2011, 7:36:39 PM8/4/11
to web2py-users
if you do not need a view and your component only displays the form
you can just "return form" and pypass the generic view. It will be
faster. I also suggest you use ajax=True. Always call the component
directly as a test that it is working.

On Aug 4, 6:33 pm, G <glenn.calt...@gmail.com> wrote:
> I think I found a workable (but slightly annoying) solution: create
> several mini-forms that only have the submit button, each with a
> different form name. Then I can just use many
> if form_blah.accepts(request.vars, session, formname='form_blah'):
> clauses. I'd still be interested in knowing if there is a better way.
> Another idea I toyed with was using jQuery to connect the .click
> action of a button to cause a jQuery.post(URL('myfunction'),
> {button_name : "yes"}) to send the button press to myfunction, but I'm
> new to jQuery, so didn't put much time into trying it and don't know
> if it would work.
> Thanks,
> G
>
> On Aug 4, 2:23 am, pbreit <pbreitenb...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Coupla other approaches:http://www.johnnycode.com/blog/2010/04/08/jquery-form-serialize-doesn......

G

unread,
Aug 4, 2011, 7:44:57 PM8/4/11
to web2py-users
Hi Massimo,
Thank you for the suggestions. I have implemented both, but neither
helps with the original problem of having components with multiple
buttons work with ajax=True. My new controller generates several forms
each with just one button and with distinct formnames. It has an if
form.accepts clause for each form to do the required action, and then
returns all the forms to the view to be properly formatted. It seems a
bit clumsy, but also seems to be working.
Thanks again,
G

On Aug 4, 4:36 pm, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Massimo Di Pierro

unread,
Aug 5, 2011, 6:34:41 AM8/5/11
to web2py-users
I know. They were just regular good practice suggestions. Sorry. Will
look for a solution.

G

unread,
Aug 5, 2011, 11:24:54 AM8/5/11
to web2py-users
Having many small forms is actually working out pretty well, and seems
to be a resonable solution. It was not intuitive to me, but I think
it's just a disconnect between the way I would do things in a GUI
framework versus the way things need to be done for web applications.
By the way, I think it would be helpful to many people to have a
tutorial that approaches web2py (or another framework) from the point
of transitioning from a GUI framework to the web. As it is, the web2py
book seems to assume a lot of familiarity with creating web
applications and less familiarity with Python. I imagine several
people like me are in the opposite situation.
Thanks for the help and the great work,
G

On Aug 5, 3:34 am, Massimo Di Pierro <massimo.dipie...@gmail.com>

Gour-Gadadhara Dasa

unread,
Aug 5, 2011, 1:12:55 PM8/5/11
to web...@googlegroups.com
On Fri, 5 Aug 2011 08:24:54 -0700 (PDT)
G <glenn....@gmail.com> wrote:

> As it is, the web2py book seems to assume a lot of familiarity with creating
> web applications and less familiarity with Python. I imagine several people
> like me are in the opposite situation.

At the moment I'm on the verge of learning bout both web development and GUI
(py+qt), and although I skimmed thropugh Django book, I really prefer web2py's
style and (hope) to undestand more about the subject. However, I assume, it
mostly has to do with web2py's design itself and now I'm waiting for a new book
to appear 'cause I like holding books in my hands and learning in that way.

The only problem I have with web2py so far, is not getting reply to my
query...hopefully there will be some answer at SO.


Sincerely,
Gour


--
“In the material world, conceptions of good and bad are
all mental speculations…” (Sri Caitanya Mahaprabhu)

http://atmarama.net | Hlapicina (Croatia) | GPG: 52B5C810


signature.asc
Reply all
Reply to author
Forward
0 new messages