Home » Django, Python

Serving Dynamic Images from Django error

2 October 2007 4 Comments

**Edit: I did blatanly miss something, which was pointed out to me on django irc#, which is that you can’t call binary data into a template. You create a view and then call the view from your template. I will leave the below for prosterity and in case it trips someone else up. It was my blonde moment.

Forgive me if I am blatantly missinged something (which is very likelyobviously), but since the unicode integration into django, I can’t seem to generate non-text images from a view, into a template ie: then django books says:

from django.http import HttpResponse

def my_image(request):
image_data = open(“/home/moneyman/public_html/media/img/main_bg.jpg”, “rb”).read()
return HttpResponse(image_data, mimetype=”image/png”)

Which works fine, but when you try and render it to a template ie:

def new(request):
image_data = open(“/home/moneyman/public_html/media/img/main_bg.jpg”, “rb”).read()
return render_to_response(‘loans/loan_reports.html’, {image_data:’image_data’}, mimetype=”image/jpeg”)

You end up with something like this:

UnicodeDecodeError at /somewhere/
‘ascii’ codec can’t decode byte 0xff in position 0: ordinal not in range(128)Request Method: GET
Request URL: http://www.somewhere.com/somewhere/
Exception Type: UnicodeDecodeError
Exception Value: ‘ascii’ codec can’t decode byte 0xff in position 0: ordinal not in range(128)
Exception Location: /home/moneyman/lib/python2.4/site-packages/django/template/context.py in __getitem__, line 38
Python Executable: /home/moneyman/bin/python
Python Version: 2.4.3

Traceback (most recent call last):
File “/home/moneyman/lib/python2.4/site-packages/django/template/__init__.py” in render_node
810. result = node.render(context)
File “/home/moneyman/lib/python2.4/site-packages/django/template/__init__.py” in render
846. return self.filter_expression.resolve(context)
File “/home/moneyman/lib/python2.4/site-packages/django/template/__init__.py” in resolve
577. obj = self.var.resolve(context)
File “/home/moneyman/lib/python2.4/site-packages/django/template/__init__.py” in resolve
704. return self._resolve_lookup(context)
File “/home/moneyman/lib/python2.4/site-packages/django/template/__init__.py” in _resolve_lookup
727. current = current[bit]
File “/home/moneyman/lib/python2.4/site-packages/django/template/context.py” in __getitem__
38. if key in d:

UnicodeDecodeError at /somewhere/
‘ascii’ codec can’t decode byte 0xff in position 0: ordinal not in range(128)

I don’t know if it’s me or a bug. I haven’t been able to get any decent feedback from #django IRC channel as yet

Technorati Tags: , ,

Powered by ScribeFire.

4 Comments »

  • James said:

    Hitting the same issue but being a novice to all things Django don’t really understand the solution, how would I go about calling a separate view from the template to bring in the binary image? Cheers

  • salubrium (author) said:

    Hi James,

    In the django book, it gives an example like my first example.

    http://www.djangobook.com/en/1.0/chapter11/

    Now, the mistake I made and probably like yourself also, is I then tried to use that code and integrate it into an existing view and then render the image as a part of that view.

    The simplest way to get this working is use the above example from the djangobook and then in the template you want the image displayed in, just use html to call the view that displays the image ie:

    < img src=”/path/to/img_view” />

    I hope that clarifies it.

  • avinash said:

    can u be more explixit wat do you mean by /path/to

  • Got it working said:

    Found this snippet looking to do the same thing and get stuck in the same place. So for full clarity:

    def pageview(request):
    return render_to_response(….)

    def graph:
    graph = graphdata
    return HttpResponse (graph, ‘image/png’)

    then in views:

    pageview:

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.