强制Django的{%static%}以静默方式失败

2024-10-01 07:10:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用自定义静态文件存储:

class StaticFilesStorage(CachedFilesMixin, S3BotoStorage):
    pass

(cachedFileMixin用于缓存破坏)。你知道吗

我的问题是,当我的模板中有{% static "file_that_does_not_exist.jpg" %}时,模板没有呈现,我得到一个异常

ValueError: The file 'file_that_does_not_exist.jpg' could not be found with <StaticFilesStorage object at 0x03AE4C70>.

它失败是因为文件不在S3上,所以S3BotoStorage无法提供服务,但这意味着如果徽标.jpg文件由于某种原因丢失了,然后整个网站都瘫痪了,这不好。。你知道吗

有没有办法强迫{% static %}默默地失败?你知道吗


Tags: 文件模板that静态notstaticpassclass
1条回答
网友
1楼 · 发布于 2024-10-01 07:10:53

现在这对我来说很有效:

class StaticFilesStorage(CachedFilesMixin, S3BotoStorage):
    def exists(self, name):
        return True

这会覆盖S3BotoStorage.exists(),因此{% static %}不会引发异常并生成s3url(这会将404提供给浏览器,但没关系)。你知道吗

相关问题 更多 >