如何从Web2py中的DB表中放置图像?

时间:2011-10-09 09:38:15

标签: python web2py

我正在尝试将图像放入视图部分我已经定义了一个表,但似乎{{for table in tables:}}不起作用。我的代码是:

default.py (控制器)

def index():
    return dict(message=T('Hello World'))

def Tables(): 
   return dict(tables=db().select(db.table.ALL))

def download():
    return response.download(request, db)

db.py (型号)

db.define_table('table',
          Field('image', type='upload')

就是这样,我在放置图片之前尝试{{for table in tables:}},但它说tables is not defined.我在{{pass}}之后使用了for。你能帮帮我一点吗?

干杯

1 个答案:

答案 0 :(得分:0)

您是否正在尝试修改哪个视图?

从您的代码

它将运作良好:

在模型中

db.define_table('mytable',
          Field('image', type='upload'))

# I do not recommend calling a table 'table' it is can be a reserved keyword
在dontrollers / default.py

def tables(): 
   return dict(tables=db().select(db.mytable.ALL))

在views / default / tables.html

{{for table in tables:}}
    <img src="{{=URL('default', 'download', args=table.image)}}" /> <br />
{{pass}}