How to display an image in GAE datastore?

前端 未结 1 1448
借酒劲吻你
借酒劲吻你 2021-01-03 15:08

I read the tutorial and all the sources I could find about displaying an image saved in datastore and still I could not make it work. I appreciate any help. This is my previ

1条回答
  •  执念已碎
    2021-01-03 15:51

    You are pointing the image source to a not defined wrong img route .
    The correct link should point to /image like this:

    
    

    I've tested your code with my correction and it works nicely:

    from google.appengine.ext import db
    from google.appengine.api import users
    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    import logging
    
    
    class HomePage(db.Model):
        thumbnail = db.BlobProperty()
        firm_name = db.StringProperty()
    
    class ImageUpload(webapp.RequestHandler):
        def get(self):
            self.response.out.write("""
            
    """) class ImageSave(webapp.RequestHandler): def post(self): homepage = HomePage() thumbnail = self.request.get("img") firm_name = self.request.get("firm_name") homepage.thumbnail = db.Blob(thumbnail) homepage.firm_name = firm_name homepage.put() self.redirect("/imageupload") class ImageResize(webapp.RequestHandler): def post(self): q = HomepageImage.all() q.filter("firm_name", "mta") qTable = q.get() if qTable: qTable.thumbnail = db.Blob(images.resize(self.request.get("img"), 32, 32)) db.put(qTable) else: self.response.out.write("""firm not found""") self.redirect("/imageupload") class DisplayImage(webapp.RequestHandler): def get(self): query = HomePage.all() query.filter("firm_name", "mta") result = query.get() self.response.out.write("""firm name: %s""" % result.firm_name) self.response.out.write("""""" % result.key()) class Image(webapp.RequestHandler): def get(self): img_id = self.request.get("img_id") logging.info("""**************************img_id: %s**************************""" % img_id) homepage = db.get(self.request.get("img_id")) if homepage.thumbnail: self.response.headers['Content-Type'] = "image/jpg" self.response.out.write(homepage.thumbnail) else: self.response.out.write("no image") application = webapp.WSGIApplication( [ ("/imageresize",ImageResize), ("/imageupload", ImageUpload), ("/displayimage", DisplayImage), ("/imagesave", ImageSave), ("/image", Image), ], debug=True ) def main(): run_wsgi_app(application) if __name__ == "__main__": main()

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