How can I put an image from DB table in Web2py?

北战南征 提交于 2020-01-05 16:42:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!