问题描述
有一种方法可以使用 app?在设置列表中,我没有看到任何内容。
要限制文件大小,您可能需要在Web服务器中执行,而不是在Django中执行。
或者,您可以,与如果文件太大,可以引发错误:
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)
尽管这会导致连接重置错误停止处理大文件。
如果您想要限制图像大小,您可以按照自述文件中的说明保存图像的大小:
class Profile(models。模型):
user = models.ForeignKey('auth.User')
avatar = ThumbnailerImageField(
upload_to ='avatars',
resize_source = dict(size =(50, 50),crop ='smart'),
)
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:
class Profile(models.Model):
user = models.ForeignKey('auth.User')
avatar = ThumbnailerImageField(
upload_to='avatars',
resize_source=dict(size=(50, 50), crop='smart'),
)
这篇关于Django简易缩略图MAX SIZE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!