Django图像工具/枕头,空图像名称/文件路径

时间:2018-07-10 14:52:27

标签: python django django-orm pillow

我使用Django(Rest Framework)和django-image-tools(DIT)库(使用ImageField),尝试通过编程方式创建新的Image实例。

为清楚起见,django-image-tools(DIT)和Pillow都有自己的图像模型。上面的Image实例是DIT Image模型。

当尝试创建一个新的DIT图像实例(代码将在下面)时,出现以下错误:

FileNotFoundError: [Errno 2] No such file or directory: ''

这似乎暗示试图访问等于空字符串的文件路径。

完整的追溯可以在下面找到

因此,我开始研究源代码以找出问题所在。提出我的问题的最好方法是显示有问题的实际代码。但这是我的主要问题,下面的代码应该使它在最初不是很清楚:

我如何让DIT获得正确的文件路径?

完全追溯错误:

Traceback (most recent call last):
  File "manage.py", line 16, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django\core\management\__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django\core\management\__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django\core\management\base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\lot\management\commands\add_product_data.py", line 45, in handle
    self.handle_row(row, supplier)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\lot\management\commands\add_product_data.py", line 60, in handle_row
    plant_instance = self.handle_plant(row_data)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\lot\management\commands\add_product_data.py", line 124, in handle_plant
    plant_instance_data['main_image'] = self.get_img_by_url(row_data)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\lot\management\commands\add_product_data.py", line 180, in get_img_by_url
    image_instance.save(file_name, django_file, save=True)
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django_image_tools\models.py", line 257, in save
    self.checksum = md5_checksum(current_path_for_image(self))
  File "C:\Users\riksch\Documents\MEGAsync\projects\Current\greenery-marketplace\backend\venv\lib\site-packages\django_image_tools\models.py", line 318, in md5_checksum
    with open(file_path, u'rb') as fh:
FileNotFoundError: [Errno 2] No such file or directory: ''

我自己写的代码导致了错误:

    def get_img_by_url(self, row_data):
    """
    Downloads and returns an image from an url.

    :param url: url string
    :return: image
    """
    request = requests.get(row_data['image_url'])

    file_name = self.generate_img_filename(row_data)
    file_path = os.path.join(settings.TEMP_MEDIA_DIR, file_name)
    with open(file_path, "wb") as f:
        f.write(request.content)

    reopen = open(file_path, "rb")
    django_file = File(reopen)
    image_instance = Image()
    image_instance.save(file_name, django_file, save=True)
    return image_instance

我使用file_name变量的打印语句检查了它是否包含所需的结果,该结果带有文件名和后缀,例如:“ my-image.jpg”。

第257行(请参见追溯):

self.checksum = md5_checksum(current_path_for_image(self))
在这种情况下,

自身是DIT图像实例

第318行是这样的:

with open(file_path, u'rb') as fh:

file_path是通过以下代码获得的:

def current_path_for_image(imageObject):
    return imageObject.image.name

DIT models.py和Pillow models.py太大,无法在此处发布。

因此,很明显,imageObject.image.name不包含名称,至少不包含空字符串,而应包含文件路径。而且我一直在研究Pillow和DIT的models.py的代码,以查看是否可以找到该名称的到达方式,但找不到。

有人可以建议我如何解决这个问题吗?

谢谢。

0 个答案:

没有答案