首页 > 解决方案 > 使用 Whitenoise 但仍无法提供静态文件

问题描述

我正在尝试在我的产品评论网站中提供静态文件,并且我正在使用 Whitenoise,但它不起作用(无法在 /static 中找到文件)(当我在本地使用 DEFAULT = False 进行测试时,它仍然作品)

我尝试配置 wsgi 文件而不是使用白噪声中间件

这是我的设置文件中的一些代码,用于提供静态服务。

DEBUG = False

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'djangobower.finders.BowerFinder',
)

你能告诉我如何解决它吗?请原谅我的英语

我尝试再次配置设置:

DEBUG = False

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# I don't have STATICFILES_DIRS, is it wrong?
STATICFILES_STORAGE = "whitenoise.storage.CompressedStaticFilesStorage"
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'djangobower.finders.BowerFinder',
)

但它仍然无法提供静态文件

标签: djangoproductwhitenoise

解决方案


我相信你缺少的是STATICFILES_STORAGE. 这是我的settings.py相关配置。

STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"
STATIC_URL = "/static/"
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
ALLOWED_HOSTS = ["*"]

推荐阅读