Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何对 ImageField 说最大允许宽度为 500 并且只允许 *.JPGs?如果在 ImageField 中无法实现,有哪些实现方式?还是应该在 html 表单上指定?提前致谢
您可以在表单清理方法中检查这一点:
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 中访问文件的名称,从中获取扩展名并像我对宽度所做的那样验证它。