Login

save image from url to django ImageField

Author:
kriwil
Posted:
November 1, 2011
Language:
Python
Version:
1.3
Score:
1 (after 3 ratings)

Saves image from a url into django's ImageField in a model. Uses requests library.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import requests

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


def save_image_from_url(model, url):
    r = requests.get(url)

    img_temp = NamedTemporaryFile(delete=True)
    img_temp.write(r.content)
    img_temp.flush()

    model.image.save("image.jpg", File(img_temp), save=True)

More like this

  1. Browser-native date input field by kytta 1 month, 1 week ago
  2. Generate and render HTML Table by LLyaudet 1 month, 2 weeks ago
  3. My firs Snippets by GutemaG 1 month, 3 weeks ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 4 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 3 months ago

Comments

fuxter (on November 1, 2011):

ImportError: No module named requests

вхат!?

#

fuxter (on November 1, 2011):

ImportError: No module named requests

what!?

#

eos87 (on October 13, 2012):

pip install requests

#

Please login first before commenting.