affaan-m/ECC · warning · ValidationError
File too large. Max size is 5MB.
Error message
File too large. Max size is 5MB.
What it means
Raised by a Django file-upload validator (`validate_file_size`) attached to a `FileField` via the `validators=[...]` kwarg. It triggers when `value.size` (bytes on the `UploadedFile`) exceeds `5 * 1024 * 1024`. Django surfaces it as a form/model `ValidationError`, which the form layer renders next to the field or the DRF layer maps to HTTP 400.
Source
Thrown at skills/django-security/SKILL.md:424
'application/pdf': {'.pdf'},
}
def validate_file_type(value):
"""Validate file type using magic bytes and cross-check extension."""
mime = magic.from_buffer(value.read(2048), mime=True)
value.seek(0)
if mime not in ALLOWED_MIMES:
raise ValidationError('Unsupported file type.')
ext = os.path.splitext(value.name)[1].lower()
if ext not in MIME_TO_EXTENSIONS.get(mime, set()):
raise ValidationError('File extension does not match file content.')
def validate_file_size(value):
"""Validate file size (max 5MB)."""
if value.size > 5 * 1024 * 1024:
raise ValidationError('File too large. Max size is 5MB.')
# models.py
class Document(models.Model):
file = models.FileField(
upload_to='documents/',
validators=[validate_file_type, validate_file_size]
)
```
For environments where installing libmagic is difficult (e.g., minimal containers),
use the pure-Python `filetype` package as an alternative:
```python
import os
from django.core.exceptions import ValidationError
import filetype # pip install filetypeView on GitHub (pinned to 01e15490f0)
Solutions
- Confirm the intended cap and make nginx/Apache `client_max_body_size` match (or be slightly smaller than) the Django validator so oversized uploads are rejected at the edge.
- If the 5MB cap is wrong, change the constant in `validate_file_size` (and any UI hint) — keep both in sync.
- For larger uploads, switch to a chunked/presigned-upload flow (S3 direct, django-storages) instead of raising the validator cap.
- Handle the `ValidationError` in the form/view so the user gets a clear message rather than a 500.
Example fix
# before
if value.size > 5 * 1024 * 1024:
raise ValidationError('File too large. Max size is 5MB.')
# after — named constant shared with nginx config and UI hint
MAX_UPLOAD_BYTES = 5 * 1024 * 1024
if value.size > MAX_UPLOAD_BYTES:
raise ValidationError(f'File too large. Max size is {MAX_UPLOAD_BYTES // (1024*1024)}MB.') Defensive patterns
Strategy: validation
Validate before calling
# before binding the validator, reject at the edge
from django.core.exceptions import ValidationError
MAX_UPLOAD_BYTES = 5 * 1024 * 1024
def assert_under_limit(upload):
if upload.size > MAX_UPLOAD_BYTES:
raise ValidationError(f"File too large: {upload.size} > {MAX_UPLOAD_BYTES}")
return True Type guard
null
Try / catch
from django.core.exceptions import ValidationError
try:
doc.full_clean() # runs validators=[validate_file_size, validate_file_type]
except ValidationError as e:
# e.message_dict maps field -> [messages]
return render_error(form, e.message_dict) Prevention
- Set nginx `client_max_body_size` slightly below the Django cap so oversized uploads never reach Python.
- Show the size limit in the upload UI next to the file picker.
- Use one shared `MAX_UPLOAD_BYTES` constant across nginx, the validator, and the front-end.
When it happens
Trigger: A user uploads a file >5MB to a `Document.file` `FileField` that lists `validate_file_size` in its `validators`. Also fires if the validator is called manually via `field.run_validators(value)` or `full_clean()`.
Common situations: Nginx/Apache `client_max_body_size` is larger than 5MB so the request reaches Django only to be rejected by the validator; or the limit was bumped in nginx but not in the Django validator (drift). Also happens when chunks/multipart uploads reassemble over the limit.
Related errors
- Unsupported file type.
- File extension does not match file content.
- ECC_PROJECT_DIR must be a child path within /workspace.
- Unknown argument: ${arg}
- Unable to infer ECC repo root from install-state operations
AI-assisted analysis of affaan-m/ECC@01e15490f0 (2026-08-13).
Data as JSON: /api/errors/198dea1f95dc8043.
Report an issue: GitHub.