我想为使用 django-ckeditor/uploader 上传的图像创建随机 uid 文件名。
我utils.py
在同一个文件夹中创建了settings.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 吗?还是有其他方法可以做到这一点?
他们的文档说明如下:
``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.