【问题标题】:passing list to template in bottle将列表传递给瓶中的模板
【发布时间】:2013-02-14 09:44:23
【问题描述】:

我有一个包含帖子信息的列表作为元组列表(列表由元组组成) 但是我遇到了如何将其传递给瓶中模板的问题,我尝试了很多,并检查了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

【问题讨论】:

  • 看来v.tpl中缺少另一个%end

标签: python list wsgi template-engine bottle


【解决方案1】:

我已经挖掘了,发现这很好用..

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

【讨论】:

    猜你喜欢
    • 2019-01-19
    • 1970-01-01
    • 2013-04-10
    • 2012-09-27
    • 2021-07-26
    • 1970-01-01
    • 2021-09-12
    • 2011-08-17
    相关资源
    最近更新 更多