문제

I am trying to upload a profile picture for a user of my app using a form.But i get a server side error saying

AttributeError: 'module' object has no attribute 'Blob'

I followed this example given by Google where they have something like

avatar = self.request.get('img')
    greeting.avatar = db.Blob(avatar)
    greeting.put()

In my own app, i created a method in my Member class like this:

def uploadProfilePicture(self, image):
    self.profile_photo = ndb.Blob(image)
    self.put()

and then created a Handler to handle this:

class ProfilePictureHandler(webapp2.RequestHandler):
def post(self):
   usr=self.request.get('username') 
   image=self.request.get('img') 
   member=Member.get_or_insert(usr) 
   member.uploadProfilePicture(image) 

I modified the imports of my app by adding:

from google.appengine.api import images (is this all i need?)

The glaring issue here is, my app uses *ndb* while the tutorial uses *db*.What is the best way to go around this?

도움이 되었습니까?

해결책

You don't need the db.Blob for ndb - just use self.profile_photo = image.

Here's a very quick example:

class TestBlobModel(ndb.Model):
    img = ndb.BlobProperty()

class Test(webapp2.RequestHandler):
    def get(self):
        image_id = self.request.get('id')
        if image_id:
            m = TestBlobModel.get_by_id(long(image_id))
            self.response.headers['content-type'] = 'image/png'
            self.response.out.write(m.img)
        else:
            self.response.out.write("""
                <form enctype="multipart/form-data" action="/test" method="POST">
                <input type="file" name="image" />
                <input type="submit" />
                </form>""")

    def post(self):
        img_data = self.request.get('image')
        m = TestBlobModel()
        m.img = img_data
        m.put()

        html = '<a href="/test?id=%s">View your image</a>' % m.key.id()
        self.response.out.write(html)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top