How to load Blobproperty image in Google App Engine?

前端 未结 1 1418
渐次进展
渐次进展 2020-12-18 10:21

I wrote some codes.

I could save image in BobProperty.

But I cannot load image into HTML page...

source code:

class Product(db.Model):

<
相关标签:
1条回答
  • 2020-12-18 10:55

    I use an auxiliary view:

    def serve_image(request, image):
        if image == "None":
            image = ""
    
        response = HttpResponse(image)
        response['Content-Type'] = "image/png"
        response['Cache-Control'] = "max-age=7200"
        return response
    

    and in the model:

    def get_image_path(self):
        # This returns the url of serve_image, with the argument of image's pk.
        # Something like /main/serve_image/1231234dfg22; this url will return a
        # response image with the blob
        return reverse("main.views.serve_image", args=[str(self.pk)])
    

    and just use {{ model.get_image_path }} instead.

    (this is django-nonrel, but I guess you could figure out what it does)

    Also, there is a post here about this; you should check it out.

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