Created
June 20, 2016 14:27
-
-
Save dasdachs/833e42624202d9316601751b8d2dcd26 to your computer and use it in GitHub Desktop.
Save an image from the shell or programatically to Wagtail.
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
# Save an image from the shell or programatically to Wagtail | |
# If you are using a cutom Image model, please import your model | |
import os | |
from django.core.file import File | |
from wagtail.wagtailimages.models import Image | |
def save_image_to_wagtail(image): | |
""" | |
Save a image to wagtail from the shell. | |
:image: path to you image | |
""" | |
path, name = os.path.split(image) | |
# Quick chec if the image is in the media dir | |
# You can skip this if you know what you are doing | |
if 'media' not in path: | |
print('Your image {} is not in the media direcotry!'.format(name)) | |
f = open(image, 'rb') | |
i = Image(title=name, file=File(f) | |
i.save() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment