将列表传递给瓶中的模板

时间:2013-02-28 09:08:48

标签: python list wsgi template-engine bottle

我有一个列表包含帖子信息作为元组列表(列表由元组组成) 但我遇到了如何将它传递给瓶子模板的问题我已经尝试了很多,并检查了stackoverflow中的大多数问题,我找不到一个好的和明确的问题。

以下是我的尝试:

@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)

这里是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>

当我尝试这个时,我得到了错误500 ...当我检查日志时,这就是原因:

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

1 个答案:

答案 0 :(得分:4)

我已经挖了一遍,发现这个工作很好而且很棒......

<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>