Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 1 and Version 2 of StoringAndRetrievingFilesFromDatabase


Ignore:
Timestamp:
05/19/06 18:05:08 (13 years ago)
Author:
godoy
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • StoringAndRetrievingFilesFromDatabase

    v1 v2  
    11One common thing is storing files inside your database.  This is usually done with image files, so this is what I'm going to show you here.  You can adapt it to your needs.  Complementary fields -- such as alternate_text, mimetype, etc. -- can be added at will.  
     2 
     3The model: 
     4 
     5{{{ 
     6class Image(SQLObject): 
     7    class sqlmeta: 
     8        table = 'images' 
     9         
     10    fileName = UnicodeCol(length = 255, unique = True) 
     11    image = BLOBCol(notNone = True) 
     12}}} 
    213 
    314The form: 
     
    3647}}} 
    3748 
     49You can use these in your template like this: 
     50 
     51{{{ 
     52  # put this in your controller, in the method where you'll expose() the page 
     53  images = [[image.id, image.fileName] for image in 
     54             model.Image.select()] 
     55 
     56  # and add "images = images" to the returned dict 
     57}}} 
     58 
     59After adding this to your model, you can then add it to your template: 
     60 
     61{{{ 
     62  <!--! Showing all images in a list --> 
     63  <ul> 
     64    <li py:for="image_id, filename in images"><a href="/show_image/${image_id}">${filename}</a></li> 
     65  </ul> 
     66}}} 
     67 
     68If you want to show the images you put the "src" attribute from the "img" tag pointing to that URL ({{{/show_image/${image_id}}}}).