11

Is there a web2py way of displaying images from a database table?

Example:

The model:

db.define_table=('images',Field('picture', 'upload' ))

The controller:

def somefunction(): to get the image.

How exactly should I "read" a picture from the database?

The view:

<img src="{{somefunction}}" />
Brian
  • 14,610
  • 7
  • 35
  • 43
Florin
  • 3,779
  • 5
  • 18
  • 15

2 Answers2

17

As is, your model will not store the image in the database -- instead, it will store the image on the filesystem, with its new filename stored in the database (in the 'picture' field). If you want to store the image itself in the database, use the following:

db.define_table('images',
    Field('picture', 'upload', uploadfield='picture_file')
    Field('picture_file', 'blob'))

Whether you store the images on the filesystem or in the database, you can use the same method to retrieve them. The 'welcome' scaffolding application includes the following download() action in the default.py controller:

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

To retrieve an image, just do something like:

<img src="{{=URL('default', 'download', args=picture_name)}}" />

where picture_name is the value stored in the 'picture' field of the 'images' table for the particular image you want to retrieve.

For more details, see here and here.

If you need further help, try asking on the mailing list.

Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Hi Anthony, how i can save "blob" in a Key/Value Nosql storage? I don't ask for details, just a way to do it. Thanks in advance! – Locke Apr 26 '12 at 10:41
  • 1
    If I want to use the DAL, I believe the only key/value store supported is the Google App Engine datastore, and in that case, the DAL will automatically store the files in the datastore (you do not even have to explicitly create a separate blob field or specify the uploadfield argument). The DAL also has partial support for MongoDB and CouchDB, though those are document databases, and I'm not sure if DAL supports storing files in them. – Anthony Apr 26 '12 at 12:47
  • db.define_table=('images', should not have the = sign. The code does not work with it in there – Tim 333 Feb 19 '15 at 15:17
  • From a view in a different controller, I have to specify that the download function is in the default controller to get this to work: i.e. ```URL('default','download', args=picture_name)``` – Juddling Nov 29 '15 at 14:52
  • @Anthony, Are you a core web2py dev? – Pacerier May 29 '19 at 12:31
2

Alternatively, if you use web2py's default way of uploading images as files, you can use:

In models:

db.define_table('images',Field('picture','upload'))

In controllers:

def somefunction():
    pic = db(db.images).select().first().picture   #select first picture
    return dict(pic=pic)

And in the default/somefunction.html view:

{{extend 'layout.html'}}
<img  src="{{=URL( 'download', args=pic)}}" />

I know this is a while after the original question but thought it might be useful as it took me a while to figure out.

Tim 333
  • 942
  • 1
  • 8
  • 9