如何在 Django 模板中显示 couchdb 图像附件
我正在将 couchdb-python 与 Django 一起使用。我正在寻找一种在模板中显示图像(作为文档附件存储在数据库中)的方法。奇怪的是,我在网上找不到任何关于如何执行此操作的示例。
目前,在views.py中我有这样的内容:
def displaypage(request,id):
docs = SERVER['docs']
try:
doc = docs[id]
except ResourceNotFound:
raise Http404
...
attachments = doc['_attachments']['someimage.jpg']
...
text_marked_down = markdown.markdown(doc['text'])
return render_to_response('couch_docs/display.html',{'row':doc,'attachments':attachments,'doctext':text_marked_down,...},context_instance=RequestContext(request))
然后,在模板display.html中:
{% extends 'site_base.html' %}
{% block wrapper %}
{{ attachments }}
<div>{{ doctext|safe }}</div>
{{ endblock }}
我看到文本很好,但对于图像我只看到以下内容: {u'stub':True, u'length':27018,u'revpos':19,u'content_type': u'image/jpeg'}
所以,显然我没有传递实际图像,或者没有正确显示它反正。奇怪的是,我在网上找不到如何实际执行此操作的示例。谁能给我指出一个,或者在这里提供吗?
I am using couchdb-python with Django. I am looking for a way to display an image (which is stored in the database as an attachment to a document) in a template. Oddly, I cannot find any example online of how to do this.
Currently, in the views.py I have something like this:
def displaypage(request,id):
docs = SERVER['docs']
try:
doc = docs[id]
except ResourceNotFound:
raise Http404
...
attachments = doc['_attachments']['someimage.jpg']
...
text_marked_down = markdown.markdown(doc['text'])
return render_to_response('couch_docs/display.html',{'row':doc,'attachments':attachments,'doctext':text_marked_down,...},context_instance=RequestContext(request))
Then, in the template display.html:
{% extends 'site_base.html' %}
{% block wrapper %}
{{ attachments }}
<div>{{ doctext|safe }}</div>
{{ endblock }}
I am seeing the text just fine, but for the image I only see the following:
{u'stub':True, u'length':27018,u'revpos':19,u'content_type': u'image/jpeg'}
So, clearly I am not passing the actual image, or not displaying it correctly anyway. Oddly, I cannot find an example online anywhere of how to actually do this. Can anyone point me to one, or provide it here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在使用模板引擎来呈现 HTML 文档。该文档将由 Web 浏览器解释,就像任何其他 HTML 文档一样。
考虑一下 HTML 页面如何包含图像。图像永远不会内嵌在 HTML 文档本身中。 HTML 页面包含一个引用,指示浏览器单独加载图像并将其显示在适当的位置。
因此,同样,您将需要:
You are using the template engine to render an HTML document. That document will be interpreted by the web browser just like any other HTML document.
Think about how an HTML page contains an image. The image is never inline within the HTML document itself. The HTML page contains a reference to instruct the browser to separately load the image and display it in place.
So, likewise, you will need to:
一旦你深入你的数据库,你可能需要考虑构建每个文档附件的 URL,如下所示:
下面我使用 Jinja2 的模板:
希望这被证明是有用的!
once you drill down your db, you might want to consider building the url of each documents attachment as follows:
And below im using Jinja2's templating:
Hope this proves usefull!