views:

1374

answers:

4

Hi, please excuse me for my ugly english ;-)

Imagine this very simple model :

class Photo(models.Model):
    image = models.ImageField('Label', upload_to='path/')

I would like to create a Photo from an image URL (i.e., not by hand in the django admin site).

I think that I need to do something like this :

from myapp.models import Photo
import urllib

img_url = 'http://www.site.com/image.jpg'
img = urllib.urlopen(img_url)
# Here I need to retrieve the image (as the same way that if I put it in an input from admin site)
photo = Photo.objects.create(image=image)

I hope that I've well explained the problem, if not tell me.

Thank you :)

Edit :

This may work but I don't know how to convert content to a django File :

from urlparse import urlparse
import urllib2
from django.core.files import File

photo = Photo()
img_url = 'http://i.ytimg.com/vi/GPpN5YUNDeI/default.jpg'
name = urlparse(img_url).path.split('/')[-1]
content = urllib2.urlopen(img_url).read()

# problem: content must be an instance of File
photo.image.save(name, content, save=True)
A: 

ImageField is just a string, a path relative to your MEDIA_ROOT setting. Just save the file (you might want to use PIL to check it is an image) and populate the field with its filename.

So it differs from your code in that you need to save the output of your urllib.urlopen to file (inside your media location), work out the path, save that to your model.

Oli
+5  A: 

from myapp.models import Photo
import urllib
from urlparse import urlparse
from django.core.files import File

img_url = 'http://www.site.com/image.jpg'

photo = Photo()    # set any other fields, but don't commit to DB (ie. don't save())
name = urlparse(img_url).path.split('/')[-1]
content = urllib.urlretrieve(img_url)

# See also: http://docs.djangoproject.com/en/dev/ref/files/file/
photo.image.save(name, File(open(content[0])), save=True)

pithyless
Hi, thank you for helping me ;). The problem is (I quote the doc) : "Note that the content argument must be an instance of File or of a subclass of File." So do you have any solution to create File instance with your content ?
Piaume
Check the new edit; this should now be a working example (although I have not tested it)
pithyless
What about my model, where i have other fields aswell. Like url, etc, etc. If id do model.image.save(...). how do I save the other fields? They cant be nullEDIT: woudl it be something like this? >>> car.photo.save('myphoto.jpg', contents, save=False)>>> car.save()
Harry
+3  A: 

I just created http://www.djangosnippets.org/snippets/1890/ for this same problem. The code is similar to pithyless' answer above except it uses urllib2.urlopen because urllib.urlretrieve doesn't perform any error handling by default so it's easy to get the contents of a 404/500 page instead of what you needed. You can create callback function & custom URLOpener subclass but I found it easier just to create my own temp file like this:

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urllib2.urlopen(url).read())
img_temp.flush()

im.file.save(img_filename, File(img_temp))
Chris Adams
A: 

Hi, I have this very same situation (of which I think is a cool set of solutions) but I'd like to manipulate the image with before sending storing it.

For example:

1) upload

2) resize

3) save

I've managed to create a PIL Image() from the uploaded content. How do I go about converting the Image() to a File() so I can input into the save method?

Joe J