tipsywacky
tipsywacky

Reputation: 3464

Uploading and displaying images in python gae?

Having a hard time to upload and display image in python gae.

Here is what I did. 1. in the app.yaml file, I added:

handlers:

- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: /(.*\.(gif|png|jpg))
  static_files: /\1
  upload: (.*\.(gif|png|jpg))

- url: .*
  script: main.app
  1. Then put down:

in

def Character(db.Model):
   avatar = db.BlobProperty()

under my handler:

if avatar:
  character_info.avatar = db.Blob(images.resize(avatar, 240,360))
   character_key = character_info.put()

I can see a binary input in gae admin data viewer, but not sure exactly how it works to call it. Then, I passed the characters object into the PageHandler and called a for loop to get each avatar.

{% for character in characters %}
#{{another html page is render here}}
{% endfor %}

tried these, but None of them works.

<img src="/images?img_id={{character.key()}}"></img>
<img src="/images/header.jpg"></img>
<img src="/avatar?avatar_id={{character.key()}}"></img>

So my questions are:

  1. How do you determine "/images"(this is the folder?) or "img_id" are the right syntax to use?

  2. I don't see any image uploaded to my localhost, is that correct?

  3. Calling an image directly from a folder like <img src="/images/header.jpg"></img> doesn't work...(in php it's works....)

Thanks in advance, any help will be appreciated!

Upvotes: 2

Views: 330

Answers (2)

tipsywacky
tipsywacky

Reputation: 3464

Alright, I figured out what I did wrong was I didn't add an image handler.

So

class ImageHandler(Handler):
    def get(self):
        character = db.get(self.request.get('img_id'))
        if character.avatar:
            self.response.headers['Content-Type'] = 'image/png'
            self.response.out.write(character.avatar)
        else:
            self.response.out.write("No image")

This solves my problem.

Upvotes: 0

Stuart Langley
Stuart Langley

Reputation: 7054

static files are served by the infrastructure and not by your application. It looks to me like you have an error in your handler definition ... it should look more like (note: no leading / in the upload: section)

- url: /(.*\.(gif$|png$|jpg$))
  static_files: /\1
  upload: (.*\.(gif$|png$|jpg$))

Then any url that ends in gif, png or jpg will be server by the app engine infrastructure and not you applications handlers.

Upvotes: 3

Related Questions