This question has been flagged
1 Reply
1904 Views

I am trying to understand how to reference/retrieve information from other views on the website i.e. retrieve forum posts from another page. I can see how the forum page retrieves and displays the post but I can't see whether it is possible to reference the forum posts on another page, simply copying the same xml leads to a 500 server error as it cannot reach the data.

If you don't understand what I mean I can provide examples or clarify further.

Avatar
Discard
Best Answer
<ul t-foreach="request.env['forum.post'].search([])" t-as='p'>
    <li><span t-field='p.name'/> </li>
</ul>


The forum is probably a bad example since 'a post' is not in a separated template...


But, just to give you an idea...


<ul t-foreach="request.env['forum.post'].search([('forum_id', '=', 1)])" t-as='p'>
<t t-call='website_forum.post_description_full'>
<t t-set='question' t-value='p'/>
<t t-set='user' t-value='request.env.user'/>
<t t-set='forum' t-value="request.env['forum.forum'].browse(1)"/>
<t t-set='reversed' t-value="lambda x: x"/>
<t t-set='searches' t-value="{}"/>
<t t-set='header' t-value="{}"/>
</t> 
</ul>

<t t-set='posts' t-value="request.env['forum.post'].search([('parent_id', '=' , False), ('forum_id', '=', 1)], order='create_date desc', limit=10)"/>
<h1> <t t-esc='len(posts)'/> lastest posts</h1>
<ul t-foreach="posts" t-as='p'>
    <li><span t-field='p.name' /></li>
</ul>

KR

Avatar
Discard
Author

This is exactly what I was after, thanks. I wanted to display some forum posts on another page just as a sort of taste of what is on the forums. I don't suppose you would know how to iterate through the forum posts and only display the first few?

Simply use the first example, and design yourself your box.

<t t-set='posts' t-value="request.env['forum.post'].search([('parent_id', '=' , False), ('forum_id', '=', 1)], order='create_date desc', limit=10)"/>

<h1> <t t-esc='len(posts)'/> lastest posts</h1>

<ul t-foreach="posts" t-as='p'>

<li><span t-field='p.name' /></li>

</ul>