问题
I'm trying to put an image in view section I have defined a table but it seems{{for table in tables:}}
doesn't work. My code is:
default.py (Controller)
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 (model)
db.define_table('table',
Field('image', type='upload')
This is it and I tried to do {{for table in tables:}}
before putting the image, but it says, the tables is not defined.
I used {{pass}}
after for
. Can you guys help me a bit?
Cheers
回答1:
Are you trying to edit which view?
from your code it will work well:
in model
db.define_table('mytable',
Field('image', type='upload'))
# I do not recommend calling a table 'table' it is can be a reserved keyword
in dontrollers/default.py
def tables():
return dict(tables=db().select(db.mytable.ALL))
in views/default/tables.html
{{for table in tables:}}
<img src="{{=URL('default', 'download', args=table.image)}}" /> <br />
{{pass}}
来源:https://stackoverflow.com/questions/7702625/how-can-i-put-an-image-from-db-table-in-web2py