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:
我在Django管理界面的CKEditor中上传了一些图像。我可以通过单击编辑器中的“图像”按钮来浏览和选择图像,然后在弹出的窗口中单击“浏览服务器”。这是弹出窗口的屏幕截图:
My question is: How can I delete images on the server?
我的问题是:如何删除服务器上的图像?
2 个解决方案
#1
1
I'm afraid this is not possible with ckeditor (at least I see no such functionality out-of-the-box).
我担心使用ckeditor是不可能的(至少我看不到开箱即用的这种功能)。
You can use a django file manager
您可以使用django文件管理器
https://www.djangopackages.com/grids/g/file-managers/
For example, django-filebrowser.
例如,django-filebrowser。
#2
2
Unfortunately this is true. Django-ckeditor does not provide any built-in solution for that problem. Information about uploaded files are not stored anywhere.
不幸的是,这是事实。 Django-ckeditor没有为该问题提供任何内置解决方案。有关上传文件的信息不会存储在任何地方
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):
使用重写的删除方法创建适当的数据模型(或使用任何可以处理文件删除的“智能字段”,Django删除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:
更改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.
这就是全部......现在,如果您注册新的UploadedFile模型,您将可以直接从Django Admin面板浏览,搜索和删除任何上传的图片。
(This solution was implemented for Django 1.10 with extension django-ckeditor 5.3 )
(此解决方案是为Django 1.10实现的,扩展名为django-ckeditor 5.3)
#1
1
I'm afraid this is not possible with ckeditor (at least I see no such functionality out-of-the-box).
我担心使用ckeditor是不可能的(至少我看不到开箱即用的这种功能)。
You can use a django file manager
您可以使用django文件管理器
https://www.djangopackages.com/grids/g/file-managers/
For example, django-filebrowser.
例如,django-filebrowser。
#2
2
Unfortunately this is true. Django-ckeditor does not provide any built-in solution for that problem. Information about uploaded files are not stored anywhere.
不幸的是,这是事实。 Django-ckeditor没有为该问题提供任何内置解决方案。有关上传文件的信息不会存储在任何地方
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):
使用重写的删除方法创建适当的数据模型(或使用任何可以处理文件删除的“智能字段”,Django删除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:
更改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.
这就是全部......现在,如果您注册新的UploadedFile模型,您将可以直接从Django Admin面板浏览,搜索和删除任何上传的图片。
(This solution was implemented for Django 1.10 with extension django-ckeditor 5.3 )
(此解决方案是为Django 1.10实现的,扩展名为django-ckeditor 5.3)