调整缩略图django Heroku,'后端不支持绝对路径'

Jas*_*n B 6 django heroku pillow

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

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

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)
Run Code Online (Sandbox Code Playgroud)

有什么东西我不见了吗?我需要告诉别的吗?

Vla*_*lad 9

使用Heroku和AWS,您只需将FileField/ImageField'path'的方法更改为'name'即可.所以在你的情况下它将是:

image.save(self.project_thumbnail.name)
Run Code Online (Sandbox Code Playgroud)

代替

image.save(self.project_thumbnail.path)
Run Code Online (Sandbox Code Playgroud)


Jas*_*n B 2

我也在其他人的谷歌搜索的帮助下找到了答案,因为我的搜索没有找到我想要的答案。这是 Pillow 以及它如何使用绝对路径进行保存的问题,所以我转而使用存储模块作为临时保存空间,现在它可以工作了。这是代码:

from django.core.files.storage import default_storage as storage

...

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

        super(Projects, self).save()
        if self.project_thumbnail:
            size = 200, 200
            image = Image.open(self.project_thumbnail)
            image.thumbnail(size, Image.ANTIALIAS)
            fh = storage.open(self.project_thumbnail.name, "w")
            format = 'png'  # You need to set the correct image format here
            image.save(fh, format)
            fh.close()
Run Code Online (Sandbox Code Playgroud)