Displaying Blob Images in Python (App Engine)

前端 未结 3 1028
遥遥无期
遥遥无期 2020-12-19 13:35

I am unable to get the image to display on the page. I can store it just fine.

These are the handlers:

class disp_image(webapp.RequestHandler):
    d         


        
相关标签:
3条回答
  • 2020-12-19 14:12

    For one, I think you're mixing up the image key and the image binary data.

    The Template

    You should be writing the image key (string) to your template:

     <img src="/disp?key={{image_key}}" />
    

    i.e. your final html should look like the following if you just pass image.key() to your template:

     <img src="/disp?key=AC3CK3333KCCK2K213" />
    

    You can think of it as a two step process. You render your template. The user browser renders the html and does a get to your image handler. The image handler then sends the binary data.

    (Btw, I was under the impression you close your html tags with either <img ... /> or <img ...></img>, not both <img .../></img>)

    Image Handler

    In your image handler, disp_image, you should be returning the binary data of the image. It looks like you first condition is okay, but I think your else condition will just return the text "/static/unknown.gif" and not the actual binary data of the gif file.

        else:
            self.response.headers['Content-Type'] = "image/png"
            return self.response.out.write("/static/unknown.gif")
    

    You should either store unknown.gif in your datastore and refer to it by key or you should be writing the "/static/unknown.gif" url to your template in lieu of "/disp?key=..."

    Lastly, the simplest way to test whether your image handler is working is when you plug in <domain>/disp?key=... into your browser URL bar, it should load the desired image.

    0 讨论(0)
  • 2020-12-19 14:17

    You do not have to serve images yourself. Google can do the job with Google High-Performance Image Serving for free! See: https://developers.google.com/appengine/docs/python/images/functions get_serving_url(blob_key, size=None, crop=False, secure_url=None) Where you only have to get the serving url once and save it for use in your HTML templates in your Images model class.

    0 讨论(0)
  • 2020-12-19 14:22

    I am not sure, but I think you should not return from the get method at the disp_image class.

    class disp_image(webapp.RequestHandler):
        def get(self):
            key = self.request.get('key')
            image = Images.get(key)
            if image:
                self.response.headers['Content-Type'] = "image/png"
                self.response.out.write(images.image)
            else:
                self.response.headers['Content-Type'] = "image/png"
                self.response.out.write("/static/unknown.gif")
    

    Could you explain where is the template rendering called from? Where are you passing the image attribute?

    0 讨论(0)
提交回复
热议问题