thank you so much for your hints and pointers pbreit, btw, an error occured
when i tried to do it.
e.g. 1.
=== controller ===
def index():
pages = db(db.page.id > 0).select(orderby = db.page.title)
comments = db(db.comment.id > 0).select()
for comment in comments:
comment_groups[comment.page_id].append(comment)
return dict(pages = pages, comment_groups = comment_groups)
=== view ===
<ol>
{{for page in pages:}}
<li>{{=page.name}}<br>
<ol>{{for comment in comment_groups[page.id]:}}
<li>{{=comment.body}}</li>
{{pass}}
</ol>
</li>
{{pass}}
</ol>
=== traceback ===
Traceback (most recent call last):
File "/home/sugizo/web2py/gluon/restricted.py", line 181, in restricted
exec ccode in environment
File "/home/sugizo/web2py/applications/a/controllers/default.py", line 61,
in <module>
File "/home/sugizo/web2py/gluon/globals.py", line 133, in <lambda>
self._caller = lambda f: f()
File "/home/sugizo/web2py/applications/a/controllers/default.py", line 6,
in index
return dict(pages = pages, comment_groups = comment_groups)
NameError: global name 'comment_groups' is not defined
e.g. 2.
=== controller ===
def index():
pages = db(db.page.id > 0).select(orderby = db.page.title)
comments = db(db.comment.id > 0).select()
return dict(pages = pages, comments = comments)
=== view ===
<ol>
{{for page in pages:}}
<li>{{=page.name}}<br>
<ol>{{for comment in comments:}}
{{if comment.page_id == page.id:}}
<li>{{=comment.body}}</li>
{{pass}}
{{pass}}
</ol>
</li>
{{pass}}
</ol>
=== traceback ===
Traceback (most recent call last):
File "/home/sugizo/web2py/gluon/restricted.py", line 181, in restricted
exec ccode in environment
File "/home/sugizo/web2py/applications/a/views/default/index.html", line
4, in <module>
<ol>{{for comment in comments:}}
File "/home/sugizo/web2py/gluon/dal.py", line 3532, in __getattr__
return self[key]
File "/home/sugizo/web2py/gluon/dal.py", line 3523, in __getitem__
return dict.__getitem__(self, key)
KeyError: 'name'
did anyone ever have the same problem to show multiple tables in 1 page?
any hints and pointers are greatly appreciate, thank you so much.
On Mon, May 16, 2011 at 9:53 AM, pbreit <[email protected]> wrote:
> Hard to say the best way to do it. This might be one way (not tested).
>
> === controller ===
> def index():
> pages = db(db.page.id>0).select(orderby = db.page.title)
> comments = db(db.comment.id>0).select()
> return dict(pages=pages, comments=comments)
>
> === view ===
> <ol>
> {{for page in page:}}
> <li>{{=page.name}}<br>
> <ol>{{for comment in comments if comment.page_id==page.id:}}
> <li>{{=comment.body}}</li>
> {{pass}}
> </ol>
> </li>
> {{pass}}
> </ol>
>
>
> There's probably a much more efficient way to do it since the "for comment
> in comments" loop goes through all comments for each page. The way you had
> it didn't really make any sense because you were getting all the pages but
> then just the comments for one (unspecified) page. "db(db.comment.page_id
> == db.page.id).select()" is going to return ALL comments. If you want the
> comments for just one page you would do something like db(db.comment.page_id
> == r <http://db.page.id/>equest.args(0)).select().
>
> To make it more efficient, you might be able to group the comments in the
> controller. I don't know if this works, it's kind of a guess.
>
> def index():
> pages = db(db.page.id>0).select(orderby = db.page.title)
> comments = db(db.comment.id>0).select()
> for comment in comments:
> comment_groups[coment.page_id].append(comment)
> return dict(pages=pages, comment_groups=comment_groups)
>
> <ol>
> {{for page in page:}}
> <li>{{=page.name}}<br>
> <ol>{{for comment in comment_groups[page.id]:}}
> <li>{{=comment.body}}</li>
> {{pass}}
> </ol>
> </li>
> {{pass}}
> </ol>
>