2013-04-29 32 views
0

对ImageField如何说最大允许宽度是500,只允许* .JPGs?如果在ImageField中无法做到这一点,那么实现它的方法是什么?或者应该在html表单上指定它? 在此先感谢在django中上传图片的最大允许尺寸和允许的扩展名?

+1

你看过http://stackoverflow.com/questions/6195478/max-image-size-on-file-upload,http://stackoverflow.com/questions/2472422/django-file-upload-size -limit – Rohan 2013-04-29 05:52:02

回答

1

您可以在形式上清洁方法检查:

def clean_picture(self): 
    # check image weight 
    image = self.cleaned_data['picture'] 
    if image: 
     if image.width > 100: 
      raise forms.ValidationError(_("Selected image is too wide")) 
    return image 

还有height属性。您可以在ImageField中访问该文件的名称,从中获取扩展名并像我以宽度进行验证。

+0

是否可以在上传前检查此问题? – Acute 2013-04-29 06:44:03

+0

你可以使用js来做到这一点,例如, G。 http://jquerybyexample.blogspot.com/2012/03/how-to-check-file-size-before-uploading.html – szaman 2013-04-30 05:11:02

+0

图像没有属性“宽度”(无论如何,我使用PIL – Acute 2013-04-30 11:06:21