Created
October 19, 2015 03:58
-
-
Save ikedumancas/566bd0f8cf1908eeeb16 to your computer and use it in GitHub Desktop.
Save Image from URL to ImageFile Django
This file contains hidden or 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
| 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