Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django上传图像的大小限制_Python_Html_Css_Django_Database - Fatal编程技术网

Python 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

我想限制保存到数据库的图像(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 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表单限制图像大小。

如果有帮助,请检查此选项。。非常感谢。