django静态文件,当托管在heroku



我的静态文件夹中有excel文件,我从中读取一些数据,但当我部署项目时,这些文件不在那里

这些是我配置静态文件的设置

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [
BASE_DIR / 'static'
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

这是我在视图中调用它们的方式

from django.conf import settings
from django.templatetags.static import static
from django.contrib.staticfiles.storage import staticfiles_storage
from django.contrib.staticfiles.finders import find
def get_static(path):
if settings.DEBUG:
return find(path)
else:
return static(path)
def sport_news(request):
path = 'excelsport_update.xlsx'
excel_data = excel(get_static(path))
return render(request, 'news/news.html', {"excel_data":excel_data, 'type':'Sport News'})

请检查您的settings.py但首先

pip install whitenoise

在中间件

'whitenoise.middleware.WhiteNoiseMiddleware',

在底部

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

然后你的主要项目url .py

if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

,请确保你有一个静态CDN文件夹在你的设置。py

然后运行python manage.py collectstatic

如果你做了上面提到的所有事情,但仍然没有文件,请确保文件在静态CDN文件夹中,如果没有在完成所有步骤后再次推送代码,你将很好地进入

如果事情仍然不适合你告诉我

最新更新