djangockeditordjango-ckeditor

How to remove images uploaded with django-ckeditor?


I have uploaded some images within the CKEditor in the Django admin interface. I can browse and select images by clicking the "Image" button in the editor, and then click "Browse Server" in the window that pops up. Here is a screenshot of the pop-up:

Screenshot of the pop-up of CKEditor

My question is: How can I delete images on the server?


Solution

  • Unfortunately this is true. Django-ckeditor does not provide any built-in solution for that problem. Information about uploaded files are not stored anywhere.

    If you want to keep them - you have to do it by yourself!

    Create the appropriate data model with the overridden delete method (or use any of ready "smart fields" which can handle removal of files for you, Django delete FileField):

    from django.db import models
    
    class UploadedFile(models.Model):
        uploaded_file = models.FileField(upload_to=u"storage/")
        uploaded_at = models.DateField(editable=False, auto_now_add=True)
    
        def __str__(self):
            return os.path.basename(self.uploaded_file.path)
    
        def url(self):
            return self.uploaded_file.url
    
        def delete(self, *args, **kwargs):
            file_storage, file_path = self.uploaded_file.storage, self.uploaded_file.path
            super(UploadedFile, self).delete(*args, **kwargs)
            file_storage.delete(file_path)
    

    Provide your own implementation of "upload" (and optional "browse") view, which will be used to memorize transactions:

    from django.conf import settings
    from django.contrib.admin.views.decorators import staff_member_required
    from django.views.decorators.cache import never_cache
    from django.views.decorators.csrf import csrf_exempt
    from ckeditor_uploader.views import upload, browse
    from .models import UploadedFile
    import re
    
    @staff_member_required
    @csrf_exempt
    def ckeditor_upload_wrapper(request, *args, **kwargs):
        response = upload(request, *args, **kwargs)
    
        if b"Invalid" not in response.content:
            try:
                matched_regex = re.search("callFunction\(\d, '(.*)'\);", str(response.content))
                file_location = matched_regex.group(1).lstrip(settings.MEDIA_URL)
                UploadedFile(uploaded_file=file_location).save()
            except Exception:
                pass
        return response
    
    @staff_member_required
    @csrf_exempt
    @never_cache
    def ckeditor_browse_wrapper(request, *args, **kwargs):
        return browse(request, *args, **kwargs)
    

    Change default redirects in urls.py:

    ...
    from app.views import ckeditor_upload_wrapper, ckeditor_browse_wrapper
    
    urlpatterns = [
        url(r'^admin/', admin.site.urls),
    
        #url(r'^ckeditor/', include('ckeditor_uploader.urls')),
        url(r'^ckeditor/upload/', ckeditor_upload_wrapper, name='ckeditor_upload'),
        url(r'^ckeditor/browse/', ckeditor_browse_wrapper, name='ckeditor_browse'),
        ...
    ]
    

    and that's all... Now if you register your new UploadedFile model, you will grant tha ability to browse, search and remove any of uploaded pictures directly from the Django Admin panel.

    (This solution was implemented for Django 1.10 with extension django-ckeditor 5.3 )