Eng Mohamed Ramzy Eng Mohamed Ramzy - 3 months ago 18
Python Question

How to upload images and storing it in db google app engine

Hello i want to know how to upload a picture from the user and store it in data base using this form ( using google data store)

<form method='post'>
<input type='file' name='img'>
<input type='submit'>


i have made a db model

from google.appengine.ext import db
class Photo(db.model):
profile_pic = db.BlobProperty()


and when i display it in the web app the name only appears

ex : example.png

i want to know how to upload it using

from google.appengine.ext import db


not

from google.appengine.ext import ndb

Answer

You can use the blobstore and a BlobReferenceProperty.

class Image(db.Model): 
    primary_image = blobstore.BlobReferenceProperty()

Then you have an upload handler where you can add image using http post:

class UploadHandler(BaseRequestHandler,
               blobstore_handlers.BlobstoreUploadHandler):
   def post(self):

        for upload in self.get_uploads():
            try:
                img = Image()
                img.primary_image = upload.key()
                img.put()
Comments