【问题标题】:Django max_upload_size gets ignoredDjango max_upload_size 被忽略
【发布时间】:2018-12-29 13:00:30
【问题描述】:

我有这段代码,但由于某种原因,文件大小被忽略了,即使我在 formatChecker.py 中直接将其设置为 ('max_upload_size', 5242880),在上传完成后,该值似乎也被忽略了。

settings.py

MAX_UPLOAD_SIZE = "5242880"

formatChecker.py

from django.db.models import FileField
from django.forms import forms
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from myproject.settings import MAX_UPLOAD_SIZE


class ContentTypeRestrictedFileField(FileField):
    """
    Same as FileField, but you can specify:
        * content_types - list containing allowed content_types. Example: ['application/pdf', 'image/jpeg']
        * max_upload_size - a number indicating the maximum file size allowed for upload.
            2.5MB - 2621440
            5MB - 5242880
            10MB - 10485760
            20MB - 20971520
            50MB - 5242880
            100MB 104857600
            250MB - 214958080
            500MB - 429916160
    """

    def __init__(self, *args, **kwargs):
        self.content_types = kwargs.pop('content_types', [])

        super(ContentTypeRestrictedFileField, self).__init__(*args, **kwargs)

    def clean(self, *args, **kwargs):
        data = super(ContentTypeRestrictedFileField, self).clean(*args, **kwargs)

        file = data.file
        try:
            content_type = file.content_type
            if content_type in self.content_types:
                if file._size > int(MAX_UPLOAD_SIZE):
                    raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (
                        filesizeformat(MAX_UPLOAD_SIZE), filesizeformat(file._size)))
            else:
                raise forms.ValidationError(_('Filetype not supported.'))
        except AttributeError:
            pass
        return data

models.py

...
class Post(models.Model):
    postattachment = ContentTypeRestrictedFileField(
      blank=True,
      null=True,
      upload_to=get_file_path_user_uploads,
      content_types=['application/pdf',
                     'application/zip',
                     'application/x-rar-compressed',
                     'application/x-tar',
                     'image/gif',
                     'image/jpeg',
                     'image/png',
                     'image/svg+xml',
                     ]
     )
...

知道为什么会出现这个问题吗? 我是不是在这里忘记了什么?

【问题讨论】:

    标签: python django django-models field


    【解决方案1】:

    setting.py中添加MAX_UPLOAD_SIZE = "5242880"

    然后在视图文件中

    from django.conf import settings
    file._size > settings.MAX_UPLOAD_SIZE
    

    file._size > int(settings.MAX_UPLOAD_SIZE)

    在init方法中,弹出两个key,所以不存在

        def __init__(self, *args, **kwargs):
           self.content_types = kwargs.pop('content_types', [])
           self.max_upload_size = kwargs.pop('max_upload_size',[])
    

    所以删除这些行

    self.content_types = kwargs.pop('content_types', [])
    self.max_upload_size = kwargs.pop('max_upload_size', [])
    

    【讨论】:

    • 不 ... MAX_UPLOAD_SIZE = 5242880 和 MAX_UPLOAD_SIZE = "5242880" 尝试
    • 不幸的是,也无法正常工作。尝试了您的解决方案的不同变体。感觉 max_upload_size 甚至都不存在。
    • 我还要从模型中删除 max_upload_size 吗?
    • 我又试了一次,但还是不行,我更新了我的问题,你发现那里有什么问题吗?
    • 我无法删除内容类型,否则我猜应该不需要这样做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2012-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-26
    相关资源
    最近更新 更多