Skip to main content
 首页 » 编程设计

python之使用 Python、GAE 和 Tipfy,无法将 db.key 作为参数传递给图像检索

2025年05月04日12rubylouvre

好吧,我正在玩 tipfy,制作一个简单的相册。我有一个使用 webbapp 的工作解决方案,这是我的模板 browse.htm,它在两个示例中保持相同:

{% 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> 

这是我的数据库模型,也是一样的

# 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 

因此,使用 webapp,我的脚本是:

# 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.response.headers['Content-Type'] = "image/png" 
    self.response.out.write(userphoto.pic) 

所以,这非常有效。现在,尝试使用 tipfy,我这样做:

# 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') 

现在,最后一个示例无法完美运行。 它确实获取所有评论和日期并正确显示它们,但没有图片。

我非常坚持这一点,欢迎任何意见。

请您参考如下方法:

所以,我设法让它工作,解决方案是更改模板

{% for pic in photo_list %} 
  <tr> 
  <td><img src='getPic?img_id={{ pic.key }} '></img></td> 

到

{% for pic in photo_list %} 
  <tr> 
  <td><img src='getPic?img_id={{ pic.key() }} '></img></td> 

欢迎任何人评论为什么这是解决方案