Search code examples
djangodjango-ckeditor

How to dynamically create upload path for media files django-ckeditor?


I am using RichTextUploadingField in my model.

I have set upload path to CKEDITOR_UPLOAD_PATH = "uploads/"

I use this field when I am on path: path("<slug:p_slug>/<slug:m_slug>/<slug:slug>/edit_feature/", views.EditFeature.as_view(), name="edit_feature"),

I want the path of the file looks like this, after adding a file:media/<slug:p_slug>/<slug:m_slug>/<slug:slug>/<file_name>/

How to achieve this?


Solution

  • ok, I have a solution I copied whole ckeditor_uploader into my django project directory

    In ckeeditor_uploader\views.py get_upload_filename I added:

    def get_upload_filename(upload_name, request):
        referrer = request.META.get('HTTP_REFERER')
        http_origin = request.META.get('HTTP_ORIGIN') + "/"
        referrer = referrer.replace(http_origin, "")
    

    And then I modified upload_path in this function (referrer at the end)

    upload_path = os.path.join(settings.CKEDITOR_UPLOAD_PATH, user_path, date_path, referrer)
    

    More on request.META docs