Created
October 17, 2013 06:16
-
-
Save renyi/7019854 to your computer and use it in GitHub Desktop.
Django, download and store image in ImageField
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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