Search code examples
sqlweb2py

web2py default upload on an SQLFORM


I am trying to make an SQLFORM that does a default upload if the the user does not put anything in the upload section. I have the file(a .png image) saved in my static/images directory. If the user actually submits a file it gets properly but my default doesn't work. For making the default I am trying

form.vars.image_thumbnail = db.myTable.image_thumbnail.store(open(os.path.join(request.folder, 'static', 'images','defaultUpload.png'),'rb')))

which is like what I do in other cases that I want this default image in the table. Is there a good way to do this.

Ok simple explanation: I have a table with an upload field. I want to put an SQLFORM on a page to insert a new entry. I also want that form to recognize when the user has left the upload section blank and fill it in with a file that is in the static folder. Can I do this with an SQLFORM?


Solution

  • This should work:

    import os
    db.myTable.image_thumbnail.default = os.path.join(request.folder, 'static', 'images', 'defaultUpload.png')
    

    If the user doesn't upload a file, it will store a copy of default file. It handles updates too: if the user deletes existing file, it will store a copy of default file.