如何对ImageField说最大允许宽度为500且仅允许* .JPG?如果在ImageField中无法做到这一点,那么实现它的方法是什么?或者应该在html表单上指定? 提前致谢
答案 0 :(得分: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
还有高度属性。您可以在ImageField中访问该文件的名称,从中获取扩展名并像使用width一样验证它。