With Python, GAE and Tipfy, unable to pass a db.key as parameter for image retrieval
Ok, so I'm playing around with tipfy, making a simple photo-gallery. I have a working solution using webbapp, this is my template, browse.htm, which stays the same in both examples:
{% for pic in photo_list %}
<tr>
<td><img src='getPic?img_id={{ pic.key }} '></img></td>
<td>{{ pic.description }}</td>
<td>{{ pic.date }}</td>
</tr>
and this is my databsemodel, which also satys the same
# This is the datamodell for the photos
class dbPhotos(db.Model):
pic = db.BlobProperty() # The photo itself
description = db.StringProperty(multiline=True) # an optional description to each photo from the uploader
date = db.DateTimeProperty(auto_now_add=True) # date and time of upload
So, using webapp, my script is:
# the handler for the gallery page
class BrowseHandler(webapp.RequestHandler):
def get(self):
que = db.Query(dbPhotos)
photos = que.fetch(limit=100)
outstr = template.render('browse.htm', {'photo_list': photos})
handler.response.out.write(outstr)
# serve pics to template
class getPicHandler(webapp.RequestHandler):
def get(self):
userphoto = db.get(self.request.get("img_id"))
self.resp开发者_如何学运维onse.headers['Content-Type'] = "image/png"
self.response.out.write(userphoto.pic)
So, this works perfectly. Now, trying to use tipfy, i do:
# the handler for the browse-page
class browseHandler(RequestHandler):
def get(self):
que = db.Query(dbPhotos)
photos = que.fetch(limit=100)
return render_response('browse.ji', photo_list=photos)
# serve pic to view
class getPicHandler(RequestHandler):
def get(self):
id = self.request.args.get("img_id")
userphoto = db.get(id)
return Response(userphoto.pic, mimetype='image/png')
Now, this last example does not work perfectly. It does fetch all the comments and dates and displays them correctly, but no pictures.
I'm so stuck on this, any input is welcome.
So, I managed to get it to work, the solution was to change the template from
{% for pic in photo_list %}
<tr>
<td><img src='getPic?img_id={{ pic.key }} '></img></td>
To
{% for pic in photo_list %}
<tr>
<td><img src='getPic?img_id={{ pic.key() }} '></img></td>
Anoyone care to comment on why this is the solution is very welcome
精彩评论