从谷歌数据存储检索要显示的图像
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能将图片直接嵌入到模板中; HTML 不是这样工作的。您需要嵌入一个带有
src
属性的标签,该属性提供一个 URL,您的应用程序将响应该 URL 来提供该 URL。我只使用 Django 的模板部分,所以如果你使用全栈,你将不得不翻译其中一些想法,而我对此无能为力。
你的 Django 模板看起来像这样:
你需要有一个处理 /skill/get_picture/:id 的路由。被调用来处理此路由的控制器代码将如下所示:
我认为返回带有图像内容的 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 asrc
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:
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:
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.
在大多数情况下,我同意 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.
我认为这篇文章会对您有很大帮助 store-jpg-gif- png-etc-it-gae-数据存储
I think this post will help you a lot store-jpg-gif-png-etc-it-gae-datastore