首先,我对编码和自学成才非常陌生,所以模型/观点/ DOM充耳不闻(但愿意学习!)
所以我将图像作为blob(BlobProperty)保存到数据库中,现在尝试为它们提供服务。
相关代码:(为了方便阅读,我拿了一吨)
class Mentors(db.Model):
id = db.StringProperty()
mentor_id = db.StringProperty()
name = db.StringProperty()
img_file = db.BlobProperty()
class ImageHandler (webapp2.RequestHandler):
def get(self):
mentor_id=self.request.get('mentor_id')
mentor = db.GqlQuery("SELECT * FROM Mentors WHERE mentor_id = :1 LIMIT 1", mentor_id)
if mentor.img_file:
self.response.headers['Content-Type'] = "image/jpg"
self.response.out.write(mentor.img_file)
else:
self.error(404)
application = webapp2.WSGIApplication([
routes.DomainRoute('medhack.prebacked.com', medhack_pages),
webapp2.Route(r'/', handler=HomepageHandler, name='home-main'),
webapp2.Route(r'/imageit', handler=ImageHandler, name='image-handler')
],
debug=True)
class MedHackHandler(webapp2.RequestHandler):
def get(self, url="/"):
# ... bunch of code to serve template etc.
mentors_events = db.GqlQuery("SELECT * FROM Mentors_Events WHERE event_id = :1 ORDER BY mentor_type DESC, mentor_id ASC", current_event_id)
mentors = mentors_events
HTML:
{% for m in mentors %}
#here 'mentors' refers to mentors_event query, and 'mentor' refers to the mentors table above.
<img src="imageit?mentor_id={{m.mentor.mentor_id}}" alt="{{m.mentor.name}} headshot"/>
{% endfor %}
看来imageit实际上并没有被调用或路径错误或......我不知道。如此多的尝试和失败。
资源我试过但却不明白:
https://developers.google.com/appengine/articles/python/serving_dynamic_images
show images on the templates of django using google app engine
https://sites.google.com/site/usfcomputerscience/storing-and-serving-images
这似乎很接近,但我无法弄清楚如何实施。需要一个“for dummies”翻译。
How to load Blobproperty image in Google App Engine?
答案 0 :(得分:1)
在处理程序中,您将从self.request.get('mentor_id')
获取ID。但是,在模板中,您已将图片网址设置为imageit?key=whatever
- 因此参数为“key”而不是“mentor_id”。选择其中一个。
答案 1 :(得分:0)
终于明白了。
我正在使用子域名,并且没有设置那个路由,只有/ img来自www根。
我也没有正确使用网址,https://developers.google.com/appengine/articles/python/serving_dynamic_images的第15遍终于解答了我的问题。