【问题标题】:Size Restriction of Uploaded Images DjangoDjango 上传图像的大小限制
【发布时间】:2020-10-26 17:07:30
【问题描述】:

我想限制保存到数据库的图像(Base64)的大小。例如,我希望图像的最大大小为 100KB,我该怎么做?我正在使用 Django。

【问题讨论】:

标签: python html css django database


【解决方案1】:
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")

这显示了如何使用 Django 表单来限制图像大小。

【讨论】:

    猜你喜欢
    • 2015-12-13
    • 2011-03-16
    • 2011-01-29
    • 2022-10-26
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多