Search code examples
pythonlistwsgitemplate-enginebottle

passing list to template in bottle


I have a list consist of post information as list of tuples (list consist of tuples) but I faced the problem of how to pass it to template in bottle i've tried a lot,, and checked most of the Questions in stackoverflow and I could't find a good and clear question.

Here's what I have tried:

@route('/v/:name')
def page_viwer(name):
    id=db.searchU('user', name)
    result=db.searchU_forG(id[0][0])
    if len(result)>0:#if we got posts 
        return template('v',post=result)

And here is v.tpl

<html>
%for post in res:
    %for id, title, dec, pic,not_needed in post:
        <h3>{{id}}</h3>
        <h3>{{title}}</h3>
        <h3>{{dec}}</h3>
        <h3>{{pic}}</h3>
        <br/>
%end
</html>

When I tried this, I got Error 500 ... And when I checked the logs this is the reason:

%for id, title, dec, pic in post: TypeError: 'int' object is not iterable


Solution

  • I've dug around and found this works fine and great ..

    <html>
    
    <table>
      %for item in res:
        title:{{item[1]}}
        <br/>
        Decription:{{item[2]}}
        <br/>
        Picture:{{item[3]}}
        <br/>
        posted by:{{item[4]}}
        <br/>
        <br/>
      %end
    </table>
    
    </html>