Login

save image from url to django ImageField

Author:
kriwil
Posted:
November 1, 2011
Language:
Python
Version:
1.3
Tags:
django image requests remote
Score:
0 (after 2 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. Save an image to ImageField from URL by ekinertac 2 years, 7 months ago
  2. get and image object by grillermo 3 years, 2 months ago
  3. Save image in field by grillermo 3 years, 4 months ago
  4. Download an image using requests, resize it using PIL, and save it to a model ImageField. by stewx 2 months ago
  5. ImageWithThumbsField by zenx 6 years, 5 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.