python-3.x - collectstatic whitenoise 在部署到 heroku 时看不到我现有的文件
问题描述
我一直在开发我的应用程序,在 heroku 上进行多次推送,一切都很好,但是当我为我的 django 应用程序添加设置以使用 cloudinary 将上传的文件存储在生产环境中时,whitenoise 无法看到我的项目中存在的文件,我没有对我的静态文件进行任何更改,并且我之前对 heroku 的推送都还可以,但是现在一旦部署的 collectstatic 部分开始,whitenoise 就会出现看不到我的文件的问题,但它们的路径仍然可以,并且没有任何更改静态文件
我不确定它是否是导致这种情况的云设置
设置.py
...
INSTALLED_APPS = [
# my apps
'user.apps.UserConfig',
'store.apps.StoreConfig',
'pages.apps.PagesConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'whitenoise.runserver_nostatic',
'cloudinary_storage',
'django.contrib.staticfiles',
'django.contrib.sites',
# 3rd party apps
'crispy_forms',
'allauth',
'allauth.account',
'allauth.socialaccount',
'cloudinary',
#'djangorave',
#providors
'allauth.socialaccount.providers.facebook',
'allauth.socialaccount.providers.google',
]
...
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
....
STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
CLOUDINARY_STORAGE = {
'CLOUD_NAME': 'YOUR_CLOUD_NAME',
'API_KEY': 'YOUR_API_KEY',
'API_SECRET': 'YOUR_API_SECRET',
}
DEFAULT_FILE_STORAGE = 'cloudinary_storage.storage.MediaCloudinaryStorage'
显示有问题的文件的项目文件结构存在
部署到heroku时出错
解决方案
显然,根据 dj3-cloudinary-storage 文档,我错过了一个小细节,如果只想让 cloudinary 处理媒体文件,则应用程序“cloudinary_storage”、“cloudinary”位于 INSTALLED_APPS 中的“django.contrib.staticfiles”下方,如下所示
INSTALLED_APPS = [
# ...
'django.contrib.staticfiles',
'cloudinary_storage',
'cloudinary',
# ...
]
如果在 'django.contrib.staticfiles' 之前使用 'cloudinary_storage' 文档中显示的第一个排列选项完成,它会覆盖导致我上面遇到的问题的 collectstatic 命令