1

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?

rossdavidh
  • 1,966
  • 2
  • 22
  • 33

2 Answers2

2

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.
gravitron
  • 3,514
  • 1
  • 20
  • 18
  • Ok, to be clear, I know that just putting "{{ attachments }}" there won't work, I just meant that I DO have hold of a real attachment, so the validity/existence of the attached image is not the issue. What I don't know is how to display it, i.e. how to write the view. I will look into PIL. – rossdavidh May 10 '11 at 16:45
0

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!

glls
  • 2,325
  • 1
  • 22
  • 39