titoalehandro
titoalehandro

Reputation: 35

Get url of Image in GAE (Python 2.7)

I am trying to get URL of Image(blob field of GAE):

class Product(db.Model):
  name = db.StringProperty()
  price = db.FloatProperty()
  added = db.DateTimeProperty(auto_now_add=True)
  image = db.BlobProperty(default=None)


url = images.get_serving_url(movie.image)

Handler of serve image:

def result(request):
  product = Product()
  product.name = "halva"
  url = 'http://echealthinsurance.com/wp-content/uploads/2009/11/minnesota.jpg'
  product.image = db.Blob(urlfetch.Fetch(url).content)
  product.put()

  template = loader.get_template("result.html")
  context = RequestContext(request, 
                         {
        "result" : u"Add"})

  return HttpResponse(template.render(context))

But i get except:

UnicodeDecodeError:

When try to ignore this exception(that was bug in Python 2.7) I get exception in other place.

And after that i try to encode Image to 'latin-1'('utf-8' don't work):

 enc_img = movie.image.decode("latin-1")
 url = images.get_serving_url(enc_img)

Result: url has a view like binary file:

 "ÝêÓ9>èýÑNëCf Äàr0xã³3Ï^µ7±\íQÀ¡>.....ÕÝ£°Ëÿ"I¢¶L`ù¥ºûMþÒ¸ÿ+ÿL¢ï£ÿÙ' alt="" />"

How I get url to show dynamic image in template?

Upvotes: 0

Views: 425

Answers (2)

Tim Hoffman
Tim Hoffman

Reputation: 12986

You are confusing two different things here.

If you are storing your image in a db.BlobProperty (code doesn't show you are doing this, but the Schema you have is using db.BlobProperty) this means your handler has to serve the image.

However you are using image.get_serving_url, which takes a BlobKey instance which comes from storing an Image in the BlobStore https://developers.google.com/appengine/docs/python/blobstore/blobkeyclass which is a completely different thing to what you are doing.

You will need to work out what you want to do, store an image (max size 1MB) in a BlobProperty and provide a handler that can serve the image, or upload it to the BlobStore and serve it from there

Upvotes: 1

fredrik
fredrik

Reputation: 17617

images.get_serving_url takes a BlobKey. Try:

enc_img = movie.image
url = images.get_serving_url(enc_img.key())

Upvotes: 0

Related Questions