如何在 Django 模板中显示 couchdb 图像附件

发布于 2024-11-06 00:38:53 字数 956 浏览 2 评论 0原文

我正在将 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 技术交流群。

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

发布评论

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

评论(2

仙气飘飘 2024-11-13 00:38:53

您正在使用模板引擎来呈现 HTML 文档。该文档将由 Web 浏览器解释,就像任何其他 HTML 文档一样。

考虑一下 HTML 页面如何包含图像。图像永远不会内嵌在 HTML 文档本身中。 HTML 页面包含一个引用,指示浏览器单独加载图像并将其显示在适当的位置。

<img src="/path/to/image" />

因此,同样,您将需要:

  • 创建一个单独的视图,仅返回图像的二进制数据。适当设置 mime 类型。请参阅 http://effbot.org/zone/django-pil.htm 了解一些想法如何返回图像,但在您的情况下将响应的内容设置为您的图像内容。
  • 添加 标记到调用您创建的新视图的模板。

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.

<img src="/path/to/image" />

So, likewise, you will need to:

  • create a separate view that will only return the binary data of the image. Set the mime type appropriately. See http://effbot.org/zone/django-pil.htm for some ideas how to return an image, but in your case set the contents of the response to be your image content.
  • add an <img ...> tag to your template that calls the new view you created.
你的背包 2024-11-13 00:38:53

一旦你深入你的数据库,你可能需要考虑构建每个文档附件的 URL,如下所示:

def function():

    couch = couchdb.Server()    #connect to server
    db = couch['img']         #connect to database which contains docs with img attachments
    doc_id = []                #create list of id's
    http_docid = []            #create list to populate href for picture path

    for i in db:                #for each id in the db
        doc_id.append(i)       #add to the carid list
        doc = db[i]             #get the document id
        for key in (doc['_attachments']):   #for the key in the doc '_attacments' payload
            print key #just to confirm
        href_docid.append(('http://yourdbDomain/dbname/'+i+'/'+key))  #create a uri and append to a list
    return href_docid     

下面我使用 Jinja2 的模板:

     {% for img in function() %}

      <img class="some-class" src="{{ img }}">

     {% endfor %}

希望这被证明是有用的!

once you drill down your db, you might want to consider building the url of each documents attachment as follows:

def function():

    couch = couchdb.Server()    #connect to server
    db = couch['img']         #connect to database which contains docs with img attachments
    doc_id = []                #create list of id's
    http_docid = []            #create list to populate href for picture path

    for i in db:                #for each id in the db
        doc_id.append(i)       #add to the carid list
        doc = db[i]             #get the document id
        for key in (doc['_attachments']):   #for the key in the doc '_attacments' payload
            print key #just to confirm
        href_docid.append(('http://yourdbDomain/dbname/'+i+'/'+key))  #create a uri and append to a list
    return href_docid     

And below im using Jinja2's templating:

     {% for img in function() %}

      <img class="some-class" src="{{ img }}">

     {% endfor %}

Hope this proves usefull!

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