Serving Dynamic Images from Django error

**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.

Published by salubrium

I am a Systems Administrator based in Sydney, Australia with some hugely varied interests: Topics covered are Virtualization, Web Hosting, Remote Desktop, Security and Backups, PHP, Python, MVC Frameworks, SEO

Leave a comment

Your email address will not be published. Required fields are marked *