Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django上载的文件进程抛出预期的str、bytes或os.PathLike对象,而不是临时上载的文件_Django - Fatal编程技术网

Django上载的文件进程抛出预期的str、bytes或os.PathLike对象,而不是临时上载的文件

Django上载的文件进程抛出预期的str、bytes或os.PathLike对象,而不是临时上载的文件,django,Django,在保存之前,我正在尝试调整/转换上载的图像的大小: def resize_convert(image_file, size, file_full_name): os.chdir(BASE_DIR + '/convert/') file_name = os.path.splitext(os.path.basename(file_full_name))[0] file_ext = os.path.splitext(os.path.basename(image_file))[1

在保存之前,我正在尝试调整/转换上载的图像的大小:

def resize_convert(image_file, size, file_full_name):
    os.chdir(BASE_DIR + '/convert/')
    file_name = os.path.splitext(os.path.basename(file_full_name))[0]
    file_ext = os.path.splitext(os.path.basename(image_file))[1]

    files = {}

    for i in size:
        cmd = ['convert', image_file, '-resize', i, file_name + i + file_ext]
        subprocess.check_call(cmd, shell=False)

        webp_cmd = ["cwebp", "-q", "100", file_name + i + file_ext, "-o", file_name + '_' + i + '.webp']
        subprocess.check_call(webp_cmd)
        files[i] = os.getcwd() + '/' + file_name + '_' + i + '.webp'

    return files
因此,从视图中,我传递了所有必要的参数,如下所示:

 for pro in product_files:
      resize_convert(pro.file.name, ["308x412"], pro)
然后它抛出这个错误

expected str, bytes or os.PathLike object, not TemporaryUploadedFile
更新

def resize_convert(_image_file, size, file_full_name):
    image_file = _image_file.temporary_file_path()
    os.chdir(BASE_DIR + '/convert/')
    file_name = os.path.splitext(os.path.basename(file_full_name))[0]
    file_ext = os.path.splitext(os.path.basename(image_file))[1]
 ....
错误:

 File "/home/.../image.py", line 17, in resize_convert
    image_file = _image_file.temporary_file_path()
AttributeError: 'str' object has no attribute 'temporary_file_path'
ERROR:django.request: Internal Server Error: /admin/product/update/7/ (22-06-2019 23:26:21; log.py:228)
Traceback (most recent call last):
  File "/home/../.env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/../.env/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/.../.env/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/../.env/lib/python3.6/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
    return view_func(request, *args, **kwargs)
  File "/home/.../views.py", line 562, in product_update
    resize_convert(pro.file.name, ["308x412"], pro)
  File "/home/..utils/image.py", line 17, in resize_convert
    image_file = _image_file.temporary_file_path()
AttributeError: 'str' object has no attribute 'temporary_file_path'

这里的问题是,传递给函数的
image\u文件
是一个
临时上载文件
对象。不能将对象传递给子流程。 您需要为它指定文件路径

def resize_convert(image_file, size, file_full_name):
    image_file_path = image_file.temporary_file_path()
    # ...
上述方法应该有效,但我建议使用
枕头
进行图像处理。这是一个python库,您不需要使用子流程。
请参见此处以获取示例:

我在遵循您的建议后出现此错误
“str”对象没有属性“temporary\u file\u path”
您能否提供引发此错误的行?(在您输入我的更改之前)