Skip to content

Instantly share code, notes, and snippets.

@renyi
Created October 17, 2013 06:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renyi/7019854 to your computer and use it in GitHub Desktop.
Save renyi/7019854 to your computer and use it in GitHub Desktop.
Django, download and store image in ImageField
image_url = og.get('image')
if image_url:
import urllib2
from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
img_temp = NamedTemporaryFile(delete=False)
img_temp.write(urllib2.urlopen(image_url).read())
img_temp.flush()
import os
import urlparse
img_filename = os.path.basename(urlparse.urlsplit(image_url).path)
self.image.save(img_filename, File(img_temp), False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment