2011-09-25 73 views
1

我閱讀了教程和所有可以找到的關於顯示保存在數據存儲中的圖像的源代碼,但仍無法使其工作。我感謝任何幫助。 This is my previous question如何在GAE數據存儲中顯示圖像?

下面的代碼爲/displayimage顯示圖像的鏈接斷開;併爲/image它給BadKeyError: Invalid string key .根據Nick Johnson reply here我必須傳遞一個空字符串img_id但logging.info在/display image顯示此密鑰:(((result.key():)))) agpkZXZ-dGluZy0xcg8LEghIb21lUGFnZRjOCAw。謝謝你的幫助。

class HomePage(db.Model): 
    thumbnail = db.BlobProperty() 
    firm_name = db.StringProperty() 

class ImageUpload(webapp.RequestHandler): 
    def get(self): 
...   
     self.response.out.write(""" 
     <form action="/imagesave" enctype="multipart/form-data" method="post"> 
     <div><label>firm name:</label> <input type="text" name="firm_name" size=40></div> 
     <div><input type="file" name="img" /></div> 
     <div><input type="submit" value="Upload image"></div> 
     </form> 
     """) 

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("""<img src="img?img_id=%s"></img>""" % 
     #chenged this line as systempuntoout's comment to: 
     self.response.out.write("""<img src="/image?img_id=%s"></img>""" % 
     result.key()) 
     #but I still get the same error 
class Image(webapp.RequestHandler): 
    def get(self): 
... 
    #I am adding the next line to show that "img_id" is an empty string. 
    #why "img_id" empty here? 

    img_id = self.request.get("img_id") 
    logging.info("""**************************img_id: %s**************************""" % img_id) 
    #**************************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() 

回答

1

您正在將圖像源指向未定義的錯誤img路由。
正確的鏈接應指向/image這樣的:

<img src="/image?img_id=%s"></img> 

我測試你的代碼我校正和它工作得很好:

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(""" 
     <form action="/imagesave" enctype="multipart/form-data" method="post"> 
     <div><label>firm name:</label> <input type="text" name="firm_name" size=40></div> 
     <div><input type="file" name="img" /></div> 
     <div><input type="submit" value="Upload image"></div> 
     </form> 
     """) 

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("""<img src="/image?img_id=%s"></img>""" % 
     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

謝謝,我所做的改變,但我仍然得到同樣的錯誤和破碎的圖像鏈接。我通過更改更新了問題。出於某種原因,Image中的「img_id」是一個空字符串。 – Zeynel

+1

適合我;清除數據存儲區,上傳正確的圖像並給它一個鏡頭。 – systempuntoout

+1

非常感謝。現在它可以工作。這是很大的幫助,再次感謝。 – Zeynel

相關問題