Answer the question
In order to leave comments, you need to log in
How to properly save image from url to django models.ImageField?
django 1.11
Code snippet from admin.py
def save_model(self, request, obj, form, change):
r = requests.get(url, stream=True)
r.raw.decode_content = True
obj.image.save(os.path.join('img/', slugify(unidecode(obj.title)), '.jpg'), r.raw)
obj.save()
Answer the question
In order to leave comments, you need to log in
Saving the image works, but the formation of the path is done with an error
def save_model(self, request, obj, form, change):
r = requests.get(url, stream=True)
r.raw.decode_content = True
obj.image.save(os.path.join('img', '{}.jpg'.format(slugify(unidecode(obj.title)))), r.raw)
obj.save()
os.path.join
forms a path from directories and a file name, but they do not need to form the name of a single folder or file.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question