从谷歌数据存储检索要显示的图像

发布于 2024-10-23 12:50:49 字数 1154 浏览 5 评论 0原文

models.py

class Badge(db.Model):
user = db.ReferenceProperty(User, collection_name='user_badges')
skill = db.ReferenceProperty(Skill, collection_name='skill_badges')
points = db.FloatProperty(required=True)

class Skill(db.Model):
skill_id = db.StringProperty()
name = db.StringProperty()
description = db.StringProperty()
picture = db.BlobProperty(default=None)

class User(db.Model):
user_id = db.StringProperty(required=True)
nickname = db.StringProperty(required=False)
email = db.StringProperty(required=False)

views.py

user = common.get_user(request)
if not user:
    return auth_error(common.getHostURI(request), request)

html

{% for badge in user.user_badges %}
  {{ badge.skill.picture }}                            
{% endfor %}

图片数据存储条目的示例为:

3601 字节,SHA-1 = b0a110a823d936d97dba83d5c8b32c7a078d3ac4

如何从数据存储中检索此图像>如果我使用badge.skill.picture,它会返回空。

编辑: 这不起作用:

return render_to_response(template_name, locals(), context_instance=RequestContext(request, params), mimetype="image/png")

models.py

class Badge(db.Model):
user = db.ReferenceProperty(User, collection_name='user_badges')
skill = db.ReferenceProperty(Skill, collection_name='skill_badges')
points = db.FloatProperty(required=True)

class Skill(db.Model):
skill_id = db.StringProperty()
name = db.StringProperty()
description = db.StringProperty()
picture = db.BlobProperty(default=None)

class User(db.Model):
user_id = db.StringProperty(required=True)
nickname = db.StringProperty(required=False)
email = db.StringProperty(required=False)

views.py

user = common.get_user(request)
if not user:
    return auth_error(common.getHostURI(request), request)

html

{% for badge in user.user_badges %}
  {{ badge.skill.picture }}                            
{% endfor %}

An example of a datastore entry of picture is:

3601 bytes, SHA-1 = b0a110a823d936d97dba83d5c8b32c7a078d3ac4

How do i retrieve this image out of the datastore> if i use badge.skill.picture, it returns me empty.

EDIT:
This does not work:

return render_to_response(template_name, locals(), context_instance=RequestContext(request, params), mimetype="image/png")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谈下烟灰 2024-10-30 12:50:49

您不能将图片直接嵌入到模板中; HTML 不是这样工作的。您需要嵌入一个带有 src 属性的 标签,该属性提供一个 URL,您的应用程序将响应该 URL 来提供该 URL。我只使用 Django 的模板部分,所以如果你使用全栈,你将不得不翻译其中一些想法,而我对此无能为力。

你的 Django 模板看起来像这样:

{% for badge in user.user_badges %}
  <img src="/skill/get_picture/{{ badge.skill.key }}">                            
{% endfor %}

你需要有一个处理 /skill/get_picture/:id 的路由。被调用来处理此路由的控制器代码将如下所示:

from google.appengine.ext import db
from models import Skill

requested_skill = db.get(id) # id comes from the :id param in the URL
return HttpResponse(requested_skill.picture, mimetype="image/png")

我认为返回带有图像内容的 HttpResponse 可能会执行您想要的操作。您绝对不想返回另一个模板;您只想返回图像的数据,仅此而已。

You can't embed a picture directly into a template; HTML does not work that way. You will need to embed an <img> tag with a src attribute that gives a URL that your application will answer to serve the URL. I only use the Templates part of Django, so if you are using the full-stack, you will have to translate some of these ideas, and I can't help much with that.

Your Django template would look something like this:

{% for badge in user.user_badges %}
  <img src="/skill/get_picture/{{ badge.skill.key }}">                            
{% endfor %}

And you will need to have a Route that handles /skill/get_picture/:id. The controller code that is called to handle this route will look something like this:

from google.appengine.ext import db
from models import Skill

requested_skill = db.get(id) # id comes from the :id param in the URL
return HttpResponse(requested_skill.picture, mimetype="image/png")

I think that returning an HttpResponse with the content of the image may do what you want. You definitely do not want to return another template; you want to return the image's data and that's all.

南薇 2024-10-30 12:50:49

在大多数情况下,我同意 Adam Crossland 的方法,但如果图像足够小(在大多数浏览器中小于 128k,在 IE 中较小),您可以使用 Data URI 方案来提供它。

http://en.wikipedia.org/wiki/Data_URI_scheme

基本上是图像数据的 Base64 编码,用少量信息告诉浏览器如何处理数据。

I agree with Adam Crossland's method for the most situations, but if the image is small enough (<128k in most browsers, smaller in IE), you could serve it using the Data URI scheme.

http://en.wikipedia.org/wiki/Data_URI_scheme

Basically a Base64 encoding of your image data, with a small amount of info to tell the browser what to do with the data.

尐籹人 2024-10-30 12:50:49

我认为这篇文章会对您有很大帮助 store-jpg-gif- png-etc-it-gae-数据存储

I think this post will help you a lot store-jpg-gif-png-etc-it-gae-datastore

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文