我试图确保每当我在 Django 中启动新实例时都会刷新缓存。我已经实现了以下自定义存储类来禁用清单文件的严格处理:
from django.contrib.staticfiles.storage import ManifestStaticFilesStorage
class NonStrictManifestStaticFilesStorage(ManifestStaticFilesStorage):
manifest_strict = False
这与设置一起使用:
STATICFILES_STORAGE = "myapp.custom_storages.NonStrictManifestStaticFilesStorage"
虽然一切似乎都正常运行,但当文件不存在时,我会遇到
ValueError
,尽管已设置 manifest_strict = False
。在这种情况下,不会出现此错误。
有人可以帮我调试这个问题吗?作为临时解决方案,删除有问题的文件可以解决该错误,但这很容易在将来出现错误。例如,如果有人在清理代码时无意中删除了 CSS 文件,则可能会破坏后端服务器,这是必须避免的。
我遇到了类似的问题,最终实施了
class ForgivingManifestStaticFilesStorage(ManifestStaticFilesStorage):
manifest_strict = False
def hashed_name(self, name, content=None, filename=None):
try:
result = super().hashed_name(name, content, filename)
except ValueError:
# When the fille is missing, let's forgive and ignore that.
# logger.warning
result = name
return result
您可能仍然遇到 404 错误。