Forums

Django 3.5 Restricting Uploads by File Size

Hi, I'm struggling to get the File Size restrictions on upload to work correctly. If I set it to a mall value under 250 bytes it appears to work but even at 1MB it allows larger files to be uploaded, so obviously the below settings and form validation combo are not working. Any help is much appreciated.

I know python anywhere has restrictions on large files anyhow but I plan on having 1000's of photos on this app so very much want to limit the file size for upload.

Settings.py

DATA_UPLOAD_MAX_MEMORY_SIZE = 2621440 #1024 * 1024 * 2  # 2MB
FILE_UPLOAD_MAX_MEMORY_SIZE = DATA_UPLOAD_MAX_MEMORY_SIZE

Forms.py

def clean_Cover_Pic(self):
        Cover_Pic = self.cleaned_data.get('CoverPic', False)
        #Cover_Pic_type = Cover_Pic.Cover_Pic_type.split('/')[0]
        #if Cover_Pic_type in settings.CONTENT_TYPES:
        if Cover_Pic._size > settings.FILE_UPLOAD_MAX_MEMORY_SIZE:
            raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.DATA_UPLOAD_MAX_MEMORY_SIZE), filesizeformat(Cover_Pic._size)))
        else:
                raise forms.ValidationError(_('File type is not supported'))
        return Cover_Pic

Note I've tried DATA_UPLOAD_MAX_MEMORY_SIZE in the form Def as well.

All of the stuff that I could find that used the _size property seemed to be really old. More recent Django versions appear to just use size :https://docs.djangoproject.com/en/3.1/ref/files/uploads/#