Is there a limit for file size on Django ImageField?

There's no direct limit that could be applied in a field of type ImageField. Although, the documentation of ImageField does not specify a way to limit the size of an image there is an alternative way. We can specify a size limit using FILE_UPLOAD_MAX_MEMORY_SIZE field in the Django settings file.

In the settings.py file you can specify the FILE_UPLOAD_MAX_MEMORY_SIZE in the desired size (in bytes). Then, Django will make sure that the image is smaller than the specified size before it gets streamed to the file system. In other words, Django will calculate the image size before saving it. If the size of the image is bigger than the FILE_UPLOAD_MAX_MEMORY_SIZE Django will discard the image.

For example, if you want an image to be smaller than 3MB, you'll have to specify the FILE_UPLOAD_MAX_MEMORY_SIZE to 3145728.

Note that if you don't specify this variable the default value is 2.5 MB.

If you like the content here please consider subscribing.

I will create a video of this guide as well in the future. If you want to get notified when the video tutorial is ready subscribe.

If you have any comments or feedback feel free to reach me @costapiy