我写了一个小函数来检查文件是否大于设定值。但不知何故它不起作用。我已经设置了 50 MB 的限制,但即使尝试上传 100 MB 的数据,它仍然让它上传。这是我的 forms.py 类和函数
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.')