【发布时间】:2017-12-10 11:04:45
【问题描述】:
我想为使用 django-ckeditor/uploader 上传的图像创建随机 uid 文件名。
我在与settings.py 相同的文件夹中创建了utils.py:
import uuid
def get_name_uid():
ext = filename.split('.')[-1]
filename = "%s.%s" % (uuid.uuid4(), ext)
return filename
我想将此“随机”文件名添加到settings.py:
CKEDITOR_FILENAME_GENERATOR = get_name_uid()
我该怎么做?我不确定如何获取在编辑器中上传的文件名。我应该将文件名从 settings.py 传递给 utils.py 吗?还是有其他方法可以做到这一点?
他们的文档说following:
``CKEDITOR_UPLOAD_PATH = "uploads/"``
When using default file system storage, images will be uploaded to "uploads" folder in your MEDIA_ROOT and urls will be created against MEDIA_URL (/media/uploads/image.jpg).
If you want be able for have control for filename generation, you have to add into settings yours custom filename generator.
```
# utils.py
def get_filename(filename):
return filename.upper()
```
```
# settings.py
CKEDITOR_FILENAME_GENERATOR = 'utils.get_filename'
```
CKEditor has been tested with django FileSystemStorage and S3BotoStorage.
There are issues using S3Storage from django-storages.
【问题讨论】:
标签: python django file-upload django-ckeditor