gpt4 book ai didi

python - Collectstatic 创建空文件

转载 作者:太空狗 更新时间:2023-10-30 01:11:28 27 4
gpt4 key购买 nike

我正在尝试将应用程序升级到 Django 1.11,但遇到 collectstatic 问题。

旧版本:

django 1.8.17 
django-storages 1.5.1

新版本:

django 1.11.12
django-storages 1.6.6

存储:

class StaticS3BotoStorage(ManifestFilesMixin, S3BotoStorage):
location = 'static'
file_overwrite = True
preload_metadata = True

class StaticS3BotoStorage(CachedFilesMixin, S3BotoStorage):
location = 'static'
file_overwrite = True
preload_metadata = True

对于旧版本,collectstatic 工作正常,包括 collectstatic --clear

升级后,collectstatic --clear 失败(没有文件被删除)。collectstatic 会复制文件,但是,有时它会创建同一文件的两个版本。在这个特定的示例中,我得到了 base.hash1.cssbase.hash2.cssbase.hash2.css 为空,因此页面打开,但无法正确呈现。

如果我不使用 CachedFilesMixinManifestFilesMixincollectstatic 工作正常,但清除仍然失败。

我测试了 django 1.11 和 django-storages 的不同组合,但它们的行为似乎都一样。

其他人是否遇到过类似问题?

最佳答案

我们遇到了同样的问题。

我认为,潜在的问题有多个问题/来源:

  • ManifestFilesMixin 使用并重复使用 ContentFile 对象来生成散列文件并多次保存它们。无需重置 ContentFile 对象(通过对它们调用 .seek(0))。
  • S3BotoStorage 保存这些文件,而不检查它们是否在正确的位置。将其与 FileSystemStorage 进行比较:始终通过遍历文件的 .chuncks() 从头开始​​读取文件。

我们通过像这样覆盖 S3BotoStorage 解决了空文件问题:

class PatchedS3StaticStorage(S3BotoStorage):
def _save(self, name, content):
if hasattr(content, 'seek') and hasattr(content, 'seekable') and content.seekable():
content.seek(0)
return super()._save(name, content)

简而言之,我们在保存文件之前查找到文件的开头。

关于python - Collectstatic 创建空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49999582/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com