multiple controllers with one view

44 views
Skip to first unread message

lbjc...@gmail.com

unread,
Aug 19, 2018, 4:44:29 PM8/19/18
to web2py-users
I have two controllers in an app. 
One controller has functions that manage DAL tables and forms. The second has imported module that are sometimes called. They both use one view. I have tried to figure out ho to work this out but cant find ho to print results to view from the second controller. With this arrangement, I keep getting an error message:  
name 'conversations' is not defined


. My second controller has this code:
def conversations():
    row
= db(db.post.author== auth.user.id).select(db.post.id, db.post.convo, orderby=~db.post.id, limitby=(0,1)).first()
    id
= row.convo if row else None
   
return dict(id=id)


with the view.html
{{=conversations()}}

I tried looking for an example, reference previous items related to this and the only answer I got online is this

Nico de Groot

unread,
Aug 20, 2018, 2:47:30 AM8/20/18
to web2py-users
The error is shown because you only send a variable 'id' to the view. All variables used in the view should be send in the dict you return from the controller. Please check the web2py book on the website on views and controllers.

Nico

lyn2py

unread,
Aug 20, 2018, 4:51:02 AM8/20/18
to web2py-users
To choose which view, put 

response.view="view.html"

in the 2 controllers that share the same view.

And you need to return the same variables used for the view in both controllers.

Anthony

unread,
Aug 20, 2018, 8:16:21 AM8/20/18
to web2py-users
def conversations():
    row
= db(db.post.author== auth.user.id).select(db.post.id, db.post.convo, orderby=~db.post.id, limitby=(0,1)).first()
    id
= row.convo if row else None
   
return dict(id=id)


with the view.html
{{=conversations()}}


You must either include "conversations" as a key in the dict returned by the controller (so it will be available as a variable in the view), or check for the existence of "conversations" in the view before attempting to access it:

{{if 'conversations' in globals():}}
{{=conversations()}}
{{pass}}

Anthony

Lovedie JC

unread,
Aug 20, 2018, 8:41:54 AM8/20/18
to web...@googlegroups.com
Thanks. This is good 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages