pass argument/variable from controller to subcontroller

31 views
Skip to first unread message

abc_coder

unread,
Jul 17, 2012, 3:59:10 AM7/17/12
to turbo...@googlegroups.com
Hi all

It's may be very easy but I don't know how can I pass argument/variable from controller to subcontroler?

Some example code:
------------------
from example.controllers.one import OneController
from example.controllers.two import TwoController

test = u'Do some test!'

class TestController(BaseController):
    one = OneController()
    two = TwoController()

    @expose('example.templates.index')
    def index(self):
        flash(test)
        return dict(page='index')
--------------------------
I want to pass this variable/argument "test" to subcotnrollers.
When I do in subcontroller flash(test) it should flash: 'Do some test!' like it work in main controller.

How can I do this?
please help

regards


Michael Pedersen

unread,
Jul 17, 2012, 12:30:43 PM7/17/12
to turbo...@googlegroups.com
This problem is not so much TurboGears as it is Python. That's not to say we can't (or won't) help, don't worry. Just remember that this applies to all the work you could do in Python.

The variable you have listed below, "test", is actually a variable within the module. I'm pretty sure the module is example.controllers.root

This means that, in order to access the variable elsewhere, you have to import the module, and then you can access the variable. Now, in your case, you'd be trying to import the root controller into subcontrollers, and that sort of practice is normally frowned upon. You might get it to work reliably, but people won't like seeing code that imports a module that imports the original module.

I would move that variable into yet another module, possibly under your lib module. From there, in each module, you can then "from example.lib.module import test" in all your controllers, and then "flash(test)" will work as desired.



--
You received this message because you are subscribed to the Google Groups "TurboGears" group.
To view this discussion on the web visit https://groups.google.com/d/msg/turbogears/-/p6n3LTvQLcsJ.
To post to this group, send email to turbo...@googlegroups.com.
To unsubscribe from this group, send email to turbogears+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/turbogears?hl=en.



--
Michael J. Pedersen
My Online Resume: http://www.icelus.org/ -- Google+ http://plus.ly/pedersen
Google Talk: m.ped...@icelus.org -- Twitter: pedersentg

Adrian Jasiński

unread,
Jul 17, 2012, 12:52:42 PM7/17/12
to turbo...@googlegroups.com
Thanks a lot for help. I have general lack of skills in programing. I
think it help for sure. I was thinking about something like
inheritance but probably for this the variable should be inside the
class. Thanks again for response.

2012/7/17 Michael Pedersen <m.ped...@icelus.org>:
Reply all
Reply to author
Forward
0 new messages