如何删除用django-ckeditor上传的图片?

时间:2014-10-05 09:17:30

标签: django ckeditor django-ckeditor

我在Django管理界面的CKEditor中上传了一些图像。我可以通过单击编辑器中的“图像”按钮来浏览和选择图像,然后在弹出的窗口中单击“浏览服务器”。以下是弹出窗口的屏幕截图:

Screenshot of the pop-up of CKEditor

我的问题是:如何删除服务器上的图像?

3 个答案:

答案 0 :(得分:3)

不幸的是,这是事实。 Django-ckeditor没有为该问题提供任何内置解决方案。有关上传文件的信息不会存储在任何地方。

如果你想保留它们 - 你必须自己做!

使用重写的删除方法创建相应的数据模型(或使用任何可以处理文件删除的“智能字段”,{{3} }):

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)

提供您自己的“上传”实施(以及可选的“浏览”)视图,该视图将用于记忆交易:

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)

更改 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'),
    ...
]

并且全部......现在,如果您注册您的新 UploadedFile 模型,您将授予能够直接浏览,搜索和删除任何上传图片的功能。 Django Admin面板。

(此解决方案是为 Django 1.10 实现的,扩展名为 django-ckeditor 5.3

答案 1 :(得分:1)

我担心使用ckeditor是不可能的(至少我看不到开箱即用的这种功能)。

您可以使用django文件管理器

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

例如,django-filebrowser

答案 2 :(得分:1)

我们现在可以选择控制工具栏的默认行为。

将此添加到settings.py:

    CKEDITOR_CONFIGS = {
        'default': {
            'toolbar': 'Custom',
            'toolbar_Custom': [
                ['Bold', 'Italic', 'Underline'],
                ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock'],
                ['Link', 'Unlink'],
                ['RemoveFormat', 'Source']
            ]
        }
    }

https://github.com/django-ckeditor/django-ckeditor#optional-customizing-ckeditor-editor