django - 使用 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',
)
但它仍然无法提供静态文件
解决方案
我相信你缺少的是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 = ["*"]
推荐阅读
- spring-data-jpa - Neo4jTransactionManager 不允许支持自定义隔离级别
- php - 控制器在 Symfony 中未显示 WSDL /w SoapServer
- r - R中的循环分析
- java - 如何使用 PUT 进行创作
- python - 在字典中保存特定列的最大值
- c# - 自定义控件中的 Xamarin.Forms 绑定集合
- c - 使用 openmp 并行化矩阵以避免错误共享
- python - 在 Django 的 collectstatic 中自定义忽略模式列表的示例
- r - 如何使用 Geom_bar 和 Facet_grid 设置一致的标签
- android - 如何正确暂停 AudioTrack 并恢复流畅播放?