Skip to content

Instantly share code, notes, and snippets.

@eyesee1
Created October 17, 2016 20:23
Show Gist options
  • Save eyesee1/1ea8e1b90bfe632cd31f5a90afc0370c to your computer and use it in GitHub Desktop.
Save eyesee1/1ea8e1b90bfe632cd31f5a90afc0370c to your computer and use it in GitHub Desktop.
Grabbing image data from a URL and saving into Wagtail CMS - example code
from io import BytesIO
import requests
from django.core.files.images import ImageFile
from wagtail.wagtailimages.models import Image
# event is a model object, substitute your model
# filename and title are up to you
# in my model, event.event_image is a ForeignKey to wagtailimages.Image
response = requests.get(url)
image = Image(title=title, file=ImageFile(BytesIO(response.content), name=filename))
image.save()
event.event_image = image
event.save()
@viirak
Copy link

viirak commented Jan 4, 2017

thank!

@mathiasp
Copy link

Thanks a lot, just what I needed!

@alexa-v
Copy link

alexa-v commented Jul 30, 2024

Thanks, this is very useful:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment