我試圖讓有人上傳圖片(成功)時,其重定向到永久鏈接與上傳的圖像顯示。這是我到目前爲止所嘗試的。使用谷歌應用引擎服務圖像
import webapp2
import os
import re
import jinja2
from google.appengine.api import images
from google.appengine.ext import db
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env= jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape = True, extensions=['jinja2.ext.autoescape'])
PAGE_RE = r'(/(?:[a-zA-Z0-9_-]+/?)*)'
def render_str(template, **params):
t = jinja_env.get_template(template)
return t.render(params)
class BaseHandler(webapp2.RequestHandler):
def render(self, template, **kw):
self.response.out.write(render_str(template, **kw))
def render_str(self, template, **params):
params['user'] = self.user
return render_str(template, **params)
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
class ImageModel(db.Model):
patient_id = db.IntegerProperty(required = True)
patient_image = db.BlobProperty(required = True)
class NewImageHandler(BaseHandler):
def get(self):
self.render('newimage.html')
def post(self):
patient_id = int(self.request.get('patient_id'))
patient_image = self.request.POST.get('patient_image').file.read()
img = str(images.Image(patient_image))
i = ImageModel(patient_id = patient_id, patient_image = db.Blob(img))
i.put()
self.redirect('imageanalysis/%s' % str(i.key().id()))
class ImageAnalysisHandler(BaseHandler):
def get(self, image_id):
key = db.Key.from_path('ImageModel', int(image_id))
imageD= db.get(key)
self.render('imageAnalysis.html', imageD = imageD, image_key = imageD.key())
app = webapp2.WSGIApplication([
('/newimage', NewImageHandler),
('/imageanalysis/([0-9]+)', ImageAnalysisHandler),
], debug=True)
這裏是我的 'newimage.html' 形式如下:
<form method = "post" enctype="multipart/form-data" action = "/newimage">
<input type="text"name="patient_id" value = {{patient_id}} >
<input type="file" name="patient_image">
<input type="submit" style="width: 100px" 1000px value="Submit">
下面是「圖像分析
<div>{{imageD.patient_id}}</div>
<div>{{imageD.key()}}
<img src='/{{image_key}}'>
</div>
它能夠呈現HTML頁面並顯示patient_id,但無法顯示圖像。有人可以向我解釋我如何動態地提供圖像。我已經在文檔中嘗試了很多東西,但它不起作用。
謝謝澄清! – piyushg91 2013-05-01 18:48:25