开发者

Django easy-thumbnails MAX SIZE

开发者 https://www.devze.com 2023-03-22 04: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.To cap file size, you might wan

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.


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'),
    )
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号