调整缩略图django Heroku的大小,“后端不支持绝对路径"

调整缩略图django Heroku的大小,“后端不支持绝对路径

问题描述:

我已经使用Django在Heroku上部署了一个应用程序,到目前为止,它似乎仍然可以正常工作,但是在上传新缩略图时遇到了问题.我已经安装了枕头,以便在上载图像时可以调整图像的大小,并保存调整后的缩略图,而不是原始图像.但是,每次上传时,都会出现以下错误:此后端不支持绝对路径."当我重新加载页面时,新图像在那里,但是没有调整大小.我正在使用Amazon AWS来存储图像.

I've got an app deployed on Heroku using Django, and so far it seems to be working but I'm having a problem uploading new thumbnails. I have installed Pillow to allow me to resize images when they're uploaded and save the resized thumbnail, not the original image. However, every time I upload, I get the following error: "This backend doesn't support absolute paths." When I reload the page, the new image is there, but it is not resized. I am using Amazon AWS to store the images.

我怀疑这与我的models.py有关.这是我的调整大小代码:

I'm suspecting it has something to do with my models.py. Here is my resize code:

class Projects(models.Model):
    project_thumbnail = models.FileField(upload_to=get_upload_file_name, null=True, blank=True)

    def __unicode__(self):
        return self.project_name

    def save(self):
        if not self.id and not self.project_description:
            return

        super(Projects, self).save()
        if self.project_thumbnail:
            image = Image.open(self.project_thumbnail)
            (width, height) = image.size

        image.thumbnail((200,200), Image.ANTIALIAS)
            image.save(self.project_thumbnail.path)

有什么我想念的吗?我需要告诉其他吗?

Is there something that I'm missing? Do I need to tell it something else?

使用Heroku和AWS,您只需要将FileField/ImageField的路径"方法更改为名称"即可.因此,在您的情况下,它将是:

Working with Heroku and AWS, you just need to change the method of FileField/ImageField 'path' to 'name'. So in your case it would be:

image.save(self.project_thumbnail.name)

代替

image.save(self.project_thumbnail.path)