Hello,
I have been trying to figure out how to customize the storage location of my media that is placed in my ckeditor rich text field.
I have a function that I use when adding images
def get_upload_path(instance, filename): model = instance.album.model.__class__._meta name = model.verbose_name_plural.replace(' ', '_') album = instance.album.name return f'blog/images/{name}/{album}/images/{filename}'
django_ckeditor_5 defaults the images into the media directory. Is there a way to make it do something similar to what I am doing in my get_upload_path function?
In the documentation it gives this example:
class CustomStorage(FileSystemStorage): """Custom storage for django_ckeditor_5 images.""" location = os.path.join(settings.MEDIA_ROOT, "django_ckeditor_5") base_url = urljoin(settings.MEDIA_URL, "django_ckeditor_5/")
I have tried using this in my settings file and I still get the same results.
Thanks for the Help!
Hey there!
Can you please edit your post to properly format the code?
Wrap your code with the back tick ` so it will considerate indentation.
Will do. Sorry about that. Thanks for the response.
Alright I have figured out why some of the basic thing were not working. First I place this python file in my app.
storage.py
`
import os
from urllib.parse import urljoin
from django.conf import settings
from django.core.files.storage import FileSystemStorage
class CustomStorage(FileSystemStorage):
βββCustom storage for django_ckeditor_5 images.βββ
location = os.path.join(settings.MEDIA_ROOT, "ckeditor")
base_url = urljoin(settings.MEDIA_URL, "ckeditor/")
`
I then had to point to this using the CKEDITOR_5_FILE_STORAGE variable in my settings file.
CKEDITOR_5_FILE_STORAGE = "blog.storage.CustomStorage"
This allows me to store all of the images to whatever media url I set in my storage.py file.
Still working out how to make it more dynamic. I would like like to have a folder for year and folder with title of articles to be added to it.
1 Like