Django directory upload get sub-directory names
I believe this is how Django is implemented. Please refer to Django's Upload Handler doc.
It has its default upload handlers MemoryFileUploadHandler
and TemporaryFileUploadHandler
. Both of them are using the UploadedFile
for handling the files, and it has a function _set_name
, which takes the base name of the file.
Even there is a comment saying why it takes the basename:
def _set_name(self, name):
# Sanitize the file name so that it can't be dangerous.
if name is not None:
# Just use the basename of the file -- anything else is dangerous.
name = os.path.basename(name)
# File names longer than 255 characters can cause problems on older OSes.
if len(name) > 255:
name, ext = os.path.splitext(name)
ext = ext[:255]
name = name[:255 - len(ext)] + ext
self._name = name
But I think you can can write your own upload handler which doesn't take the basename and behaves as you want. Here is little info how you can write custom upload handler.
Then you need to define your handler in FILE_UPLOAD_HANDLERS
setting.
EDIT Custom Upload Handlers with Django 3.1