Skip to content

Instantly share code, notes, and snippets.

@ikedumancas
Created October 19, 2015 03:58
Show Gist options
  • Select an option

  • Save ikedumancas/566bd0f8cf1908eeeb16 to your computer and use it in GitHub Desktop.

Select an option

Save ikedumancas/566bd0f8cf1908eeeb16 to your computer and use it in GitHub Desktop.
Save Image from URL to ImageFile Django
import os
import urllib2
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
class ModelWithImage(models.Model):
image_file = models.ImageField(upload_to='images')
new_img = ModelWithImage()
img_temp = NamedTemporaryFile()
img_temp.write(urllib2.urlopen(imgURL).read())
img_temp.flush()
fname = os.path.basename(new_anime.img_url)
new_img.image_file.save(fname, File(img_temp))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment