我正在尝试通过实现文件大小限制和文件扩展名来验证上传的图像。问题是上传图像时,我想在处理之前验证文件大小限制和文件扩展名,但出现此错误
“member_descriptor”对象不可订阅
forms.py" in clean_image
43. if not file.name[-3:].lower() in ['jpg']:
Exception Type: TypeError at /pro/
Exception Value: 'member_descriptor' object is unsubscriptable
File "C:\Python26\lib\site-packages\django\forms\forms.py" in _get_errors
115. self.full_clean()
File "C:\Python26\lib\site-packages\django\forms\forms.py" in full_clean
270. self._clean_fields()
我的表格.py
class PersonForm(forms.ModelForm):
def clean_image(self):
cleaned_data = super(PersonForm,self).clean()
image = cleaned_data.get("image")
if image:
if image._size > 4*1024*1024:
raise forms.ValidationError("Image Must be <4mb Less")
if not file.name[-3:].lower() in ['jpg']:
raise forms.ValidationError("Your file extension was not recongized")
return image
class Meta:
model = Person
fields = ('image',)
视图.py
def pro(request):
form = PersonForm()
if request.POST.has_key('pro'):
form = PersonForm(request.POST,request.FILES)
if request.POST['pro'] == 'first':
if form.is_valid():
person = Person.objects.get(user=request.user)
image = form.cleaned_data['image']
if image:
person.image = form.cleaned_data['image']
person.save()
return render(request,'profile.html',{'form':form})
我的模型.py
class Person(models.Model):
user = models.ForeignKey(User)
image = models.FileField(upload_to="images/",blank=True)
def __unicode__(self):
return self.name