Python Django上传图像的大小限制
我想限制保存到数据库的图像(Base64)的大小。例如,我希望图像的最大大小为100KB,如何才能做到这一点?我正在使用Django。Python Django上传图像的大小限制,python,html,css,django,database,Python,Html,Css,Django,Database,我想限制保存到数据库的图像(Base64)的大小。例如,我希望图像的最大大小为100KB,如何才能做到这一点?我正在使用Django。类CreateForm(forms.ModelForm): class CreateForm(forms.ModelForm): #the size you want it to be, example for 2 mb is given below max_upload_limit = 2*1024*1024 #override clean func
类CreateForm(forms.ModelForm):
class CreateForm(forms.ModelForm):
#the size you want it to be, example for 2 mb is given below
max_upload_limit = 2*1024*1024
#override clean function to something like this
clean(self):
cleaned_data = super().clean()
pic = cleaned_data.get('picture')
if pic is None:
return
if len(pic) > self.max_upload_limit:
self.add_error('picture', "File must be < "+self.max_upload_limit_text+" bytes")
#下面给出了您想要的大小,例如2MB
最大上传限制=2*1024*1024
#将clean函数重写为如下内容
清洁(自我):
cleaned_data=super().clean()
pic=已清理的数据。获取('picture')
如果pic为无:
返回
如果len(pic)>self.max\u上传\u限制:
self.add\u错误(“图片”,“文件必须<”+self.max\u上载\u限制\u文本+“字节”)
这显示了如何使用Django表单限制图像大小。如果有帮助,请检查此选项。。非常感谢。