如何在Web2py中将数据库表中的图像显示出来?

1 投票
1 回答
2181 浏览
提问于 2025-04-17 03:58

我想在我定义的视图部分放一张图片,我已经设置了一个表格,但似乎{{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.我在for后面用了{{pass}}。你们能帮我一下吗?

谢谢!

1 个回答

0

你想编辑哪个视图呢?

从你的代码来看,这样做是没问题的:

在模型部分

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

# I do not recommend calling a table 'table' it is can be a reserved keyword

在控制器的default.py文件里

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

在视图的default/tables.html文件里

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

撰写回答