我正在努力做到这一点,以便当有人上传图片(成功)时,它会重定向到显示上传图片的永久链接。这是我到目前为止所尝试的。
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">
这是“图像分析”的 HTML
<div>{{imageD.patient_id}}</div>
<div>{{imageD.key()}}
<img src='/{{image_key}}'>
</div>
它能够渲染页面并显示 Patient_id,但无法显示图像。有人可以向我解释如何动态提供图像。我已经尝试了文档中的许多内容,但它不起作用。