diff --git a/django/core/validators.py b/django/core/validators.py index 38f6ec1284..1a36246171 100644 --- a/django/core/validators.py +++ b/django/core/validators.py @@ -461,6 +461,8 @@ class FileExtensionValidator: code = 'invalid_extension' def __init__(self, allowed_extensions=None, message=None, code=None): + if allowed_extensions is not None: + allowed_extensions = [allowed_extension.lower() for allowed_extension in allowed_extensions] self.allowed_extensions = allowed_extensions if message is not None: self.message = message diff --git a/docs/ref/validators.txt b/docs/ref/validators.txt index 8148dd6f7a..0136fb45a3 100644 --- a/docs/ref/validators.txt +++ b/docs/ref/validators.txt @@ -286,6 +286,7 @@ to, or in lieu of custom ``field.clean()`` methods. Raises a :exc:`~django.core.exceptions.ValidationError` with a code of ``'invalid_extension'`` if the extension of ``value.name`` (``value`` is a :class:`~django.core.files.File`) isn't found in ``allowed_extensions``. + The extension is compared case-insensitively with ``allowed_extensions``. .. warning:: diff --git a/tests/validators/tests.py b/tests/validators/tests.py index d7888ef9a7..8620e7dc35 100644 --- a/tests/validators/tests.py +++ b/tests/validators/tests.py @@ -256,6 +256,7 @@ TEST_DATA = [ (FileExtensionValidator(['']), ContentFile('contents', name='fileWithNoExtension'), None), (FileExtensionValidator(['txt']), ContentFile('contents', name='file.txt'), None), (FileExtensionValidator(['txt']), ContentFile('contents', name='file.TXT'), None), + (FileExtensionValidator(['TXT']), ContentFile('contents', name='file.txt'), None), (FileExtensionValidator(), ContentFile('contents', name='file.jpg'), None), (validate_image_file_extension, ContentFile('contents', name='file.jpg'), None), @@ -458,6 +459,14 @@ class TestValidatorEquality(TestCase): FileExtensionValidator(['txt']), FileExtensionValidator(['txt']) ) + self.assertEqual( + FileExtensionValidator(['TXT']), + FileExtensionValidator(['txt']) + ) + self.assertEqual( + FileExtensionValidator(['TXT', 'png']), + FileExtensionValidator(['txt', 'png']) + ) self.assertEqual( FileExtensionValidator(['txt']), FileExtensionValidator(['txt'], code='invalid_extension')