【发布时间】:2013-04-22 17:20:20
【问题描述】:
如何告诉 ImageField 允许的最大宽度是 500 并且只允许 *.JPGs?如果在 ImageField 中无法实现,有哪些实现方式?还是应该在 html 表单上指定? 提前致谢
【问题讨论】:
标签: django django-models
如何告诉 ImageField 允许的最大宽度是 500 并且只允许 *.JPGs?如果在 ImageField 中无法实现,有哪些实现方式?还是应该在 html 表单上指定? 提前致谢
【问题讨论】:
标签: django django-models
您可以在表单清理方法中进行检查:
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 中访问文件的名称,从中获取扩展名并像使用宽度一样验证它。
【讨论】: