Why Does This Image Not Displaying (broken Image Icon)?
I am trying to display an image that I saved to Datastore with DisplayImage handler below but I only see a broken image link. Do you know why? Thanks! class HomePage(db.Model):
Solution 1:
To serve an image from blobstore, use get_serving_url
or if you have a blobproperty you can have a look at my old code that used to serve my blobproperties from an image class back before when there was no blobstore:
classImage(db.Model):
name = db.StringProperty()
desc = db.StringProperty()
owner = db.UserProperty()
secret = db.StringProperty()
full = db.BlobProperty()
full_ext = db.StringProperty()
small = db.BlobProperty()
small_ext = db.StringProperty()
thumb = db.BlobProperty()
thumb_ext = db.StringProperty()
published = db.BooleanProperty()
added = db.DateTimeProperty(auto_now_add=True)
modified = db.DateTimeProperty(auto_now=True)
defthumb_name(self):
return'%s.%s' % (self.key(), self.thumb_ext)
defsmall_name(self):
return'%s_small.%s' % (self.key(), self.small_ext)
deffull_name(self):
return'%s_full.%s' % (self.key(), self.full_ext)
classUploadImage(webapp.RequestHandler):
defpost(self, key):
im = db.get(db.Key(key))
ifnot im:
self.error(404)
returnif self.request.POST['id'] != im.secret:
self.error(400)
return
file_data = self.request.POST['file'].file.read()
if self.request.POST['size'] == '100x100':
im.thumb = file_data
a = 'small'elif self.request.POST['size'] == '500x500':
im.small = file_data
a = 'full'if im.small and im.thumb:
im.published = True
im.save()
logging.info("%s updated %s" % (im.key(), a) )
self.response.out.write("ok")
mimetypes = {
'jpeg': 'image/jpeg',
'jpg': 'image/jpeg',
'tiff': 'image/tiff',
'tif': 'image/tiff',
'gif': 'image/gif',
'png': 'image/png',
}
classServeImage(webapp.RequestHandler):
defget(self, key, sz, ext):
im = db.get(db.Key(key))
ifnot im:
self.error(404)
returnif sz == '.':
d = im.thumb
elif sz == '_small.':
d = im.small
elif sz == '_full.':
d = im.full
else:
raise Exception('wrong sz %r' % sz)
ifnot d:
d = im.full
else:
self.response.headers.add_header("Expires", "Thu, 01 Dec 2014 16:00:00 GMT")
self.response.headers["Content-Type"] = mimetypes[ext]
self.response.out.write(d)
Solution 2:
This question was answered by systempuntoout
in my followup question. He noted that I was pointing the image source to a not defined wrong img route.
The correct link should point to /image like this:
<img src="/image?img_id=%s"></img
>
Post a Comment for "Why Does This Image Not Displaying (broken Image Icon)?"