Search code examples
djangoherokudjango-viewsdjango-staticfilesdjango-settings

django static files when hosting in heroku


I have excel files in my static folder that I read from to get some data but the files arent there when I deploy my project

these are my settings for configuring static files

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


STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

this is how I call them in my views

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 = 'excel\sport_update.xlsx'
    excel_data = excel(get_static(path))
    return render(request, 'news/news.html', {"excel_data":excel_data, 'type':'Sport News'})

Solution

  • please check in you settings.py but first

    pip install whitenoise
    

    in middleware

    'whitenoise.middleware.WhiteNoiseMiddleware',
    

    in bottom

    STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
    

    then your main projects urls.py

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

    and please make sure you do have a static cdn folder in your settings.py

    then run python manage.py collectstatic

    if you done all the thing mentioned above and still file is not please make sure that file is in the static cdn folder and if not push the code again after completing all the step and you will be good to go

    if things still did not work for you tell me