calling function from the view

3,188 views
Skip to first unread message

Don

unread,
Aug 11, 2009, 9:50:40 AM8/11/09
to web2py-users
I am new to the MVC paradigm, python, and web2py. I would like to be
able to:

1. create a controller (done)
1. define a series of functions (including index)
2. call any of the function from a single view.

Example. I have a model that consists of three tables. My default.py
controllers index function returns a dictionary containing rows from a
query about vendor names. I build a table with the vendor names. I
also want to build a subtable listing the products available from each
vendor. For that I would like to define another function that takes
the vendor id and returns products related to that vendor id. But I
would have to make another view (if I understand correctly).

I want all the information to appear in a single view. Is this
possible?

mdipierro

unread,
Aug 11, 2009, 10:54:48 AM8/11/09
to web2py-users
There are two types of frameworks push and pull.

In a push framework (like web2py, Django, Rails) the URL is mapped
into a function, which returns data (in the form of a dictionary) and
the data is rendered by one view.

In a pull framework (like Struts and JBoss) the URL is mapped into a
view which calls one or more controller functions.

From your question I assume you have a pull framework in mind. You can
mimic a pull framework in web2py in multiple ways. One way is via ajax
requests:

#controller default.py
def index(): return dict()
def f1(): return response.render('partial_view1.html',dict())
def f2(): return response.render('partial_view2.html',dict())

#view default/index.html
{{extend 'alyout.html'}}
<div id="f1"></div>
<div id="f2"></div>
<script>
jQuery(document).ready(funciton(){
ajax('{{=URL(f='f1')}}",[],'f1');
ajax('{{=URL(f='f2')}}",[],'f2');
})};
</script>

#view partial_view1.html
Hello

#view partial_view2.html
World


Hope it makes sense.

Massimo

Don Lee

unread,
Aug 21, 2009, 1:48:47 PM8/21/09
to web...@googlegroups.com
I found what appears to be an easier way.  I should have thought of it before.  Everything is an object so:

in my controller:
=====================================
def testFunction(a):
    return a*a
   
def index():
    return dict(funct=testFunction)
=====================================

in my view:

======================================
{{=funct(5)}}
======================================

results in 25 being printed on the page

Iceberg

unread,
Aug 21, 2009, 11:19:38 PM8/21/09
to web2py-users
In case you don't know, you can also put functions inside model, so
that they are globally available. I think this make your controller
and view have less coupling.

in your model:
def helperFunction(a):
return a*a

in your controller:
def index():
return {}
# much cleaner than return dict(func1=helperFunction,
func2=someOtherFunc, ...)

in your view:
{{=helperFunction(5)}}

Yebach

unread,
May 11, 2016, 8:07:00 AM5/11/16
to web2py-users
In which model file do you put it or you create a new one (if so what is the name of the file)?
Reply all
Reply to author
Forward
0 new messages