Reputation: 345
I wrote a small function that checks if the file is bigger than a set value or not. But somehow its not working. I've put my limit of 50 MB, but even if try uploading 100 MB worth data, its still letting it upload. This is my forms.py class and function
from django.template.defaultfilters import filesizeformat
class ScribbleSaveForm(forms.Form):
title=forms.CharField(
label=u'Title',
required=False,
widget=forms.TextInput(attrs={'size':120})
)
media=forms.FileField(
label=u'add file',
required=False,
widget=forms.FileInput()
)
body=forms.CharField(
label=u'description',
widget=forms.Textarea()
)
tags=forms.CharField(
label=u'Tags',
required=False,
widget=forms.TextInput(attrs={'size':64})
)
def file_size(self):
media=self.cleaned_data.get('media',False)
if media:
if media._size > 50*1024*1024 :
raise forms.ValidationError('Please keep filesize under 50MB.')
return media
else:
raise forms.ValidationError('Could not read uploaded file.')
Upvotes: 2
Views: 196
Reputation: 3800
You're on the right track, you should rename file_size to clean_media. See https://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute
Also, make sure you're always returning what you're cleaning, validates or not.
Upvotes: 2