Django easy-thumbnails MAX SIZE

旧街凉风 提交于 2019-12-11 12:22:56

问题


is there a way to set a max size for the images uploaded in my django app using easy-thumbnails app?

In the settings list I don't see anything about it.


回答1:


To cap file size, you might want to do it in the webserver, rather than in Django.

Alternatively, you can specify a custom file handler, with which you can raise an error if the file is too big:

from django.core.files.uploadhandler import TemporaryFileUploadHandler, StopUpload

class SizeLimitUploadHandler(TemporaryFileUploadHandler):
    def new_file(self, field_name, file_name, content_type, content_length, charset):
        if content_length > MAX_FILE_SIZE:
            raise StopUpload(True)

Though this will cause a connection reset error in order to stop processing the large file.

If you want to cap image size, you can resize the image before it is saved as stated in the readme:

By passing a resize_source argument to the ThumbnailerImageField, you can resize the source image before it is saved:

class Profile(models.Model):
    user = models.ForeignKey('auth.User')
    avatar = ThumbnailerImageField(
        upload_to='avatars',
        resize_source=dict(size=(50, 50), crop='smart'),
    )


来源:https://stackoverflow.com/questions/6765016/django-easy-thumbnails-max-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!