How to delete images uploaded with django-ckeditor?

I uploaded some images inside CKEditor in the Django admin interface. I can view and select images by clicking the "Image" button in the editor, and then click "Browse Server" in the window that appears. Here is a screenshot of the popup:

Screenshot of the pop-up of CKEditor

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

+6
source share
2 answers

I am afraid this is not possible with ckeditor (at least I don't see such functionality out of the box).

You can use django file manager

https://www.djangopackages.com/grids/g/file-managers/

For example, django-filebrowser .

+1
source

Unfortunately it's true. Django-ckeditor does not provide any built-in solution for this problem. Information about the downloaded files is not stored anywhere.

If you want to save them - you have to do it yourself!

Create the appropriate data model using the overridden deletion method (or use any of the ready-made smart fields that can handle file deletion 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 the β€œdownload” (and optional β€œview”), which will be used to remember 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 the 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 a new UploadedFile model, you will provide the ability to view, search and delete any uploaded images directly from the Django admin panel.

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

0
source

Source: https://habr.com/ru/post/976271/


All Articles