django 2.2.21 文件路径

问题描述

因为 django 做了这个安全问题

https://www.djangoproject.com/weblog/2021/may/04/security-releases/

我无法打开测试数据文件: 我正在使用

import os
from django.core.files import File
file = File(os.path.join(os.path.dirname(__file__),"data","tests","test_pdf.pdf"))

然后

Model.objects.create(name="test". file=file)

出现错误

 django.core.exceptions.SuspicIoUsFileOperation: File name 'apps/app/data/tests/test_pdf.pdf' includes path elements

有什么解决办法吗?或者加载 pdf 的正确方法是什么?

解决方法

重复的 Getting error "SuspiciousFileOperation" after Django Version Update Django: 3.1.9 from Django: 3.1.8

(解决方法是将 name="test_pdf.pdf" 作为 kwarg 添加到 File() 构造函数中。)

编辑:该错误现已在今天发布的 django 版本中修复,5 月 13 日,2.23,版本 2.2.23、3.1.11、3.2.3