使用python在谷歌应用引擎中的特定html页面中显示图像

时间:2015-05-07 11:12:30

标签: python google-app-engine

如何使用python中的google app引擎将从blob商店检索到的图像显示到所需的html页面?我已经将产品的详细信息与图像一起插入到ndb中,但是当我试图显示所有细节时,显示的是细节或图像,而不是在同一页面中显示它们。

from email.mime import image
import os
import logging
from google.appengine.ext import blobstore
from google.appengine.ext import ndb
from google.appengine.ext.webapp import blobstore_handlers

import jinja2
import webapp2

JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)

class ProductImage(ndb.Model):
    blob_key = ndb.BlobKeyProperty()

class productdetails(ndb.Model):
   productid = ndb.StringProperty(indexed=True)
   productdesc = ndb.StringProperty(indexed=True)
   productmrp = ndb.StringProperty(indexed=True)

class MainPage(webapp2.RequestHandler):
   def get(self):
       template = JINJA_ENVIRONMENT.get_template('index.html')
       self.response.write(template.render())

class addproduct(webapp2.RequestHandler):
   def get(self):
      upload_url = blobstore.create_upload_url('/displayproduct')
      self.response.write('''<html>
                            <head>
                            <title>Admin - Insert Product page</title>
                            </head>
                            <body>
                                <h1>Admin - Insert Product page</h1>
                                <form action="%s" method="POST"       enctype="multipart/form-data">
                            Product name:<br>
                            <input type="text" name="pname" value="">
                            <br>
                            <br>
                            Product Price:<br>
                            <input type="text" name="price" value="">
                            <br>
                            <br>
                            Product Feature:<br>
                            <input type="text" name="description" value="" >
                            <br><br>
                            Product Image:<br>
                            <input type="file" name="img">
                            <br><br>
                            <input type="submit" value="Submit">
                            </form>
                            </body>
                            </html>
                      ''' % upload_url)




  class displayproduct(blobstore_handlers.BlobstoreUploadHandler,blobstore_handlers.BlobstoreDownloadHandler,webapp2.RequestHandler):
def post(self):
    pname1 = self.request.get('pname')
    price1 = self.request.get('price')
    description1 = self.request.get('description')
    upload = self.get_uploads()[0]

    #productimage_key=ndb.get(self.request.get("img_id"))
    #productimage=productimage_key.get()
    det = productdetails(productid=pname1, productmrp=price1, productdesc=description1)
    det.put()

    template_values = {
        'n1': pname1, 'p1': price1, 'd1': description1,
    }
    template = JINJA_ENVIRONMENT.get_template('dispproduct.html')
    self.response.write(template.render(template_values))
    x=upload.key()
    ProductImage(blob_key=x).put()
    self.send_blob(x)
    #self.response.write('<div><img src=/%s></img></div>'  )




class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):
def get(self, photo_key):
    if not blobstore.get(photo_key):
        self.error(404)
    else:
        self.send_blob(photo_key)


application = webapp2.WSGIApplication([
('/', MainPage),
('/data', data),
('/addproduct', addproduct),
('/displayproduct', displayproduct),
('/view_photo/([^/]+)?', ViewPhotoHandler),
], debug=True)

0 个答案:

没有答案