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 2 and Version 3 of StoringAndRetrievingFilesFromDatabase


Ignore:
Timestamp:
03/04/08 19:19:35 (11 years ago)
Author:
Chris Arndt
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • StoringAndRetrievingFilesFromDatabase

    v2 v3  
    1 One 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.  
     1{{{ 
    22 
    3 The model: 
     3#!rst 
    44 
    5 {{{ 
    6 class Image(SQLObject): 
    7     class sqlmeta: 
    8         table = 'images' 
    9          
    10     fileName = UnicodeCol(length = 255, unique = True) 
    11     image = BLOBCol(notNone = True) 
     5.. note:: This page has been migrated to http://docs.turbogears.org/1.0/StoringAndRetrievingFilesFromDatabase 
    126}}} 
    137 
    14 The form: 
    15  
    16 {{{ 
    17         form = widgets.TableForm(fields = [ 
    18             widgets.FileField( 
    19             name = 'image', 
    20             label = _('Some picture'), 
    21             validator = validators.FieldStorageUploadConverter()) 
    22            ]) 
    23 }}} 
    24  
    25 Saving the image to the database: 
    26  
    27 {{{ 
    28     @expose() 
    29     @validate(validators = { 
    30         'image': validators.FieldStorageUploadConverter() 
    31         }) 
    32     def save(self, image): 
    33         image = model.Image( 
    34             fileName = image.filename, 
    35             image = image.file.read(), 
    36         ) 
    37         return dict(image_id = image.id) 
    38 }}} 
    39  
    40 Retrieving the image from the database: 
    41  
    42 {{{ 
    43     @turbogears.expose(content_type = "image/jpg") 
    44     def show_image(self, image_id): 
    45         # "model.Image.q.image is where the image is stored 
    46         return model.Image.get(image_id).image 
    47 }}} 
    48  
    49 You 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  
    59 After 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  
    68 If you want to show the images you put the "src" attribute from the "img" tag pointing to that URL ({{{/show_image/${image_id}}}}).