python - Django "Unable to determine the file' s size“tempfile.TemporaryFile 错误

标签 python django

我在使用标准 Django FileField 和 tempfile.TemporaryFile 时遇到问题。每当我尝试使用 TemporaryFile 保存 FileField 时,我都会收到“无法确定文件大小”错误。

例如,给定一个名为 Model 的模型、一个名为 FileField 的文件字段和一个名为 TempFile 的临时文件:

Model.FileField.save('foobar', django.core.files.File(TempFile), save=True)

这将给我上述错误。有什么想法吗?

最佳答案

我在使用 tempfile.TemporaryFile 时遇到了这个问题。当我切换到 tempfile.NamedTemporaryFile 它就消失了。我相信 TemporaryFile 只是模拟一个文件(至少在某些操作系统上),而 NamedTemporaryFile 实际上是一个文件。

关于python - Django "Unable to determine the file' s size“tempfile.TemporaryFile 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6785666/

相关文章:

python - nose2 与 py.test 隔离进程

python - 将列表中的元素收集到相等元素列表的列表中?

python - 使用 ThreadPoolExecutor 的 Map 传递多个值

python - 使用 PIL 为 Django 网站旋转通过电子邮件上传的图像

python - 按照第一个 Django 应用程序教程,我无法执行 runserver 命令

python - Django 中的元素计数

python - LiveServerTestCase - settings.Database 配置不正确

python - scrapy蜘蛛绕过拒绝我的规则

python - 模块未找到错误: importlib when pushing to Heroku despite local changes

javascript - 如何将图像位置放入 django 模板中的 javascript 代码中