Simple project to validate FileFields/ImageFields, like max size of uploaded file.
Until now, there is only one validator: MaxSizeValidator.
NOT WORKING WITH NEW DJANGO 1.10.
- Django 1.8 or higher (not tested on previous versions) but lower than 1.10!!
pip install django-file-validator
IMPORTANT: If you want use django translations, you need to put 'django_file_validator' on your INSTALLED_APPS (on settings.py). Doing this, validator's error messages will be translated (only English, Spanish and Portuguese-BR for now).
In your models, import and use MaxSizeValidator:
from django_file_validator.validators import MaxSizeValidator
class YourModel(models.Model):
. . .
image = models.ImageField( null=True, blank=True, upload_to='uploads/yourmodel/img/', validators=[MaxSizeValidator()])
. . .
You can change the max size value passing a parameter on each attibute:
from django_file_validator.validators import MaxSizeValidator
class YourModel(models.Model):
. . .
default_image = models.ImageField( null=True, blank=True, upload_to='uploads/yourmodel/img/', validators=[MaxSizeValidator()])
big_image = models.ImageField( null=True, blank=True, upload_to='uploads/yourmodel/img/', validators=[MaxSizeValidator(2048)])
small_image = models.ImageField( null=True, blank=True, upload_to='uploads/yourmodel/img/', validators=[MaxSizeValidator(256)])
. . .
- FILE_SIZE_LIMIT_IN_KILOBYTES
You can change the default max size limit of uploaded files, just putting this variable on settings.py. Default value is 512 kB.
FILE_SIZE_LIMIT_IN_KILOBYTES=512