Discussion:
Returning images with Django
(too old to reply)
Filipe
2006-09-04 11:09:40 UTC
Permalink
Hi all,

I need to make some images available on my django website that are
stored on an internal file server. The idea would be that Django would
be listening to requests for those images, the respective view would
read the bytes for the requested image (which would be available
locally via samba) and return the bytes to the web browser.

Can the HttpResponse returned by a View object contain an image? are
there any examples?
Is this possible and the best way to do it?

thanks in advance


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
Ian Holsman
2006-09-04 11:13:15 UTC
Permalink
im = Image.open('%s/blah.jpg' % settings.MEDIA_ROOT )
resp = HttpResponse( mimetype="image/jpeg" )
im.save( resp, 'JPEG' )
return resp

?
Post by Filipe
Hi all,
I need to make some images available on my django website that are
stored on an internal file server. The idea would be that Django would
be listening to requests for those images, the respective view would
read the bytes for the requested image (which would be available
locally via samba) and return the bytes to the web browser.
Can the HttpResponse returned by a View object contain an image? are
there any examples?
Is this possible and the best way to do it?
thanks in advance
--
Ian Holsman
***@Holsman.net
http://garden-gossip.com/ -- what's in your garden?



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
Filipe
2006-09-05 09:53:38 UTC
Permalink
Post by Ian Holsman
im = Image.open('%s/blah.jpg' % settings.MEDIA_ROOT )
resp = HttpResponse( mimetype="image/jpeg" )
im.save( resp, 'JPEG' )
return resp
?
yeah, precisely! thanks.
although it works perfectly there's something I don't quite understand
(i'm still a newbie to python). Isn't save() supposed to receive a
filename as the first argument? We're passing it a HttpResponse...


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
Ned Batchelder
2006-09-05 10:09:51 UTC
Permalink
Post by Filipe
Post by Ian Holsman
im = Image.open('%s/blah.jpg' % settings.MEDIA_ROOT )
resp = HttpResponse( mimetype="image/jpeg" )
im.save( resp, 'JPEG' )
return resp
?
yeah, precisely! thanks.
although it works perfectly there's something I don't quite understand
(i'm still a newbie to python). Isn't save() supposed to receive a
filename as the first argument? We're passing it a HttpResponse...
From the documentation for HttpResponse
(http://www.djangoproject.com/documentation/request_response/):

But if you want to add content incrementally, you can use response as a
Post by Filipe
Post by Ian Holsman
response = HttpResponse()
response.write("<p>Here's the text of the Web page.</p>")
response.write("<p>Here's another paragraph.</p>")
This is an example of Python's "duck typing". Because HttpResponse
implements the same methods as a file, it can be used in any place that
a file can be used. PIL's save() method expects a file, but all it does
is call write() (and maybe some other file methods). HttpResponse
implements those methods with appropriate semantics, so it's known as
"file-like", and can be used as a file can be used.

--Ned.
--
Ned Batchelder, http://nedbatchelder.com



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
Filipe
2006-09-05 16:58:48 UTC
Permalink
I see. thanks.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

charles sibbald
2006-09-04 11:46:49 UTC
Permalink
im doing something similar, I have set up a link from my webserver which is not a very big machine, to my NAS box using standard linux networking protocols...

this may not be the best way to do it, but it works

----- Original Message ----
From: Filipe <***@gmail.com>
To: Django users <django-***@googlegroups.com>
Sent: Monday, September 4, 2006 12:09:40 PM
Subject: Returning images with Django


Hi all,

I need to make some images available on my django website that are
stored on an internal file server. The idea would be that Django would
be listening to requests for those images, the respective view would
read the bytes for the requested image (which would be available
locally via samba) and return the bytes to the web browser.

Can the HttpResponse returned by a View object contain an image? are
there any examples?
Is this possible and the best way to do it?

thanks in advance









--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users-***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
Continue reading on narkive:
Loading...