python 45 lines · 2 tabs

Django file upload handling with validation

Priya Sharma Jan 2026
2 tabs
from django.core.exceptions import ValidationError


def validate_file_size(file):
    """Limit file size to 5MB."""
    max_size_mb = 5
    if file.size > max_size_mb * 1024 * 1024:
        raise ValidationError(f'File size cannot exceed {max_size_mb}MB')


def validate_image_extension(file):
    """Allow only specific image formats."""
    allowed_extensions = ['.jpg', '.jpeg', '.png', '.gif']
    file_extension = file.name.lower().split('.')[-1]

    if f'.{file_extension}' not in allowed_extensions:
        raise ValidationError(
            f'File extension .{file_extension} is not allowed. '
            f'Allowed extensions: {", ".join(allowed_extensions)}'
        )
2 files · python Explain with highlit

File uploads require careful validation for security. I validate file size using a custom validator and check content type. Using FileField or ImageField, Django handles storage automatically. I configure MEDIA_ROOT and MEDIA_URL for development. For production, I use django-storages with S3 or similar. The upload_to parameter can be a callable for dynamic paths. I generate unique filenames to avoid collisions. For large files, I consider chunked uploads or background processing. Always validate file content, not just extension, to prevent malicious uploads.


Related snips

Share this code

Here's the card — post it anywhere.

Django file upload handling with validation — share card
Link copied