Re: how to call a controller funtion on click of a button?

2,283 views
Skip to first unread message

Leonel Câmara

unread,
Nov 8, 2012, 11:32:46 PM11/8/12
to web...@googlegroups.com
Set the button onclick attribute to run a javascript function which calls it using ajax, or, depending on what you're doing, using a trapped link can be a good idea (styled to look like a button if you want, just add class btn to it) finally also consider if you could be doing this using the LOAD helper.

You can read more about trapped links and load here: http://web2py.com/books/default/chapter/29/12

The best way, really depends on what you're trying to achieve.


  
 
Quinta-feira, 8 de Novembro de 2012 19:52:44 UTC, web4py escreveu:
hi
i have a button in my html file on click of which i want to call a function present in default.py unable to find any source...

davedigerati

unread,
Aug 3, 2013, 5:38:40 PM8/3/13
to web...@googlegroups.com
Leonel, can you elaborate on your answer, I'm stuck on a similar problem.

I need a button in html to execute a function in my default.py controller.  In that fx it performs logic to determine where exactly to redirect, as well as some housekeeping.

This thread was close but involved trapped links loading pages which I am not interested in, I just need a function called.

I feel like this is probably reeeeeally simple, but as always the simplest is the hardest;)
Thanks

Anthony

unread,
Aug 3, 2013, 7:44:57 PM8/3/13
to web...@googlegroups.com

I need a button in html to execute a function in my default.py controller.  In that fx it performs logic to determine where exactly to redirect, as well as some housekeeping.

What have you tried? Can you show some code? Sounds like you just need an ajax call, and possibly a redirect(..., client_side=True) if you want the end result to be a redirect of the whole page.

Anthony 

davedigerati

unread,
Aug 3, 2013, 11:07:52 PM8/3/13
to web...@googlegroups.com
Well as for 'trying' anything I'm still searching for the method that fits...

So the usual button to redirect to another page I use this

<button type="button" class="btn btn-primary" onclick="location.href='{{=URL('gameTime')}}'">Done with my squares</button>


What I am trying to do is click a button that does NOT redirect.  If I use a function in defauly.py that has no view in the URL field above, it loads the generic view as advertised.  Grrr.  Isn't there just a simple way to suppress calling the generic view?  Just leave the view and retreat into the controller for a little code parsing? ;)

I'd go down the trapped link route but as I understand it that still involves loading the page, just not displaying it, and while I could go create a pretend page it feels too much work- like there ought to be a switch on the URL helper to just execute code...?

Anthony

unread,
Aug 4, 2013, 8:21:52 AM8/4/13
to web...@googlegroups.com

So the usual button to redirect to another page I use this

<button type="button" class="btn btn-primary" onclick="location.href='{{=URL('gameTime')}}'">Done with my squares</button>


What I am trying to do is click a button that does NOT redirect.  If I use a function in defauly.py that has no view in the URL field above, it loads the generic view as advertised.  Grrr.  Isn't there just a simple way to suppress calling the generic view?  Just leave the view and retreat into the controller for a little code parsing? ;)

First, generic views are disabled by default (though enabled on localhost in the welcome app via a line in db.py). Second, views are only called if your controller action returns a dictionary -- so just don't return a dictionary.

<button type="button" class="btn btn-primary" onclick="jQuery.ajax('{{=URL('gameTime')}}');">Done with my squares</button>

def gameTime():
    [do something]
    return 'OK'

Anthony

davedigerati

unread,
Aug 4, 2013, 1:46:44 PM8/4/13
to web...@googlegroups.com
STUPENDOUS!  Thank you Anthony!  Is there a wiki or Q&A site somewhere for these little tidbits of knowledge?  The manual is fantastic but too exhaustive for a quick answer for me anyway... if not maybe I'll build one on web2py;)

Anthony

unread,
Aug 4, 2013, 3:10:17 PM8/4/13
to web...@googlegroups.com
There's www.web2pyslices.com for recipes, articles, and plugins/modules/apps (recently added). It also has Q&A functionality, though most questions get asked here (and to some extent on Stack Overflow).

Anthony
Reply all
Reply to author
Forward
0 new messages