diff --git a/django/core/files/storage.py b/django/core/files/storage.py index 4c27fce6052..e9166d94ff0 100644 --- a/django/core/files/storage.py +++ b/django/core/files/storage.py @@ -291,7 +291,7 @@ class FileSystemStorage(Storage): os.chmod(full_path, self.file_permissions_mode) # Store filenames with forward slashes, even on Windows. - return name.replace('\\', '/') + return str(name).replace('\\', '/') def delete(self, name): assert name, "The name argument is not allowed to be empty." diff --git a/docs/ref/files/storage.txt b/docs/ref/files/storage.txt index b2710062df1..542eb2dc7ed 100644 --- a/docs/ref/files/storage.txt +++ b/docs/ref/files/storage.txt @@ -69,6 +69,11 @@ The ``FileSystemStorage`` class time of the last metadata change, and on others (like Windows), it's the creation time of the file. +.. versionchanged:: 3.1 + + Support for :class:`pathlib.Path` was added to the + ``FileSystemStorage.save()`` method. + The ``Storage`` class ===================== diff --git a/docs/releases/3.1.txt b/docs/releases/3.1.txt index d928abd2057..ef2ab9b1733 100644 --- a/docs/releases/3.1.txt +++ b/docs/releases/3.1.txt @@ -124,7 +124,7 @@ Email File Storage ~~~~~~~~~~~~ -* ... +* ``FileSystemStorage.save()`` method now supports :class:`pathlib.Path`. File Uploads ~~~~~~~~~~~~ diff --git a/tests/file_storage/tests.py b/tests/file_storage/tests.py index 779116b3bd2..be6e11de93c 100644 --- a/tests/file_storage/tests.py +++ b/tests/file_storage/tests.py @@ -545,7 +545,7 @@ class FileStorageTests(SimpleTestCase): p = Path('test.file') self.assertFalse(self.storage.exists(p)) f = ContentFile('custom contents') - f_name = self.storage.save(str(p), f) + f_name = self.storage.save(p, f) # Storage basic methods. self.assertEqual(self.storage.path(p), os.path.join(self.temp_dir, p)) self.assertEqual(self.storage.size(p), 15) @@ -560,10 +560,10 @@ class CustomStorage(FileSystemStorage): """ Append numbers to duplicate files rather than underscores, like Trac. """ - basename, *ext = name.split('.') + basename, *ext = os.path.splitext(name) number = 2 while self.exists(name): - name = '.'.join([basename, str(number)] + ext) + name = ''.join([basename, '.', str(number)] + ext) number += 1 return name