Home:ALL Converter>Heroku static files not loading, Django

Heroku static files not loading, Django

Ask Time:2015-03-10T18:23:56         Author:Tapasweni Pathak

Json Formatter

I'm trying to push my Django project to Heroku, but it isn't loading the staticfiles.

I used this to setup the things, everything is fine but I'm not able to fix the issue with static files.

My directory structure is like this

help_the_needy
    help_the_needy
        __init__.py
        settings.py
        urls.py
        views.py
        wsgi.py
    manage.py
    Procfile  
    requirements.txt  
    static
        css
        font-awesome
        fonts  
        img  
        js
    templates
        base.html
        display_list2.html
        index.html

Here is the complete code (all files).

This is my settings.py.

I tried alot of things to fix this, but nothing seems to work.

When I push it does copy static files but it's not loading them.

Can someone please point me to my mistake? Where is it wrong?

Author:Tapasweni Pathak,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/28961177/heroku-static-files-not-loading-django
AakashRajni :

pip install whitenoise \n\nAdd whitenoise to requirement.txt.\nand also add whitenoise in the middleware of settings.py\nMIDDLEWARE = [\n 'django.middleware.security.SecurityMiddleware',\n 'whitenoise.middleware.WhiteNoiseMiddleware', #add whitenoise\n 'django.contrib.sessions.middleware.SessionMiddleware',\n 'django.middleware.common.CommonMiddleware',\n 'django.middleware.csrf.CsrfViewMiddleware',\n 'django.contrib.auth.middleware.AuthenticationMiddleware',\n 'django.contrib.messages.middleware.MessageMiddleware',\n 'django.middleware.clickjacking.XFrameOptionsMiddleware',\n]\n\nFollow the steps in https://devcenter.heroku.com/articles/django-assets",
2017-12-14T10:28:31
Daniel Roseman :

Your STATICFILES_DIRS setting is wrong. It should be pointing to the actual location of the \"static\" directory containing the files:\n\nSTATICFILES_DIRS = (\n os.path.join(BASE_DIR, 'static'),\n)\n",
2015-03-10T13:27:33
t_io :

It looks like django don't know where your static_root is.\nThis folder is autogenerated when you fire manage.py collectstatic\nMake sure that the folders static and templates are inside your django app. You didn't created a django app to put this folders into. You created a django project the next step is to create a django app with something like this python manage.py startapp polls\n\n# Absolute filesystem path to the Django project directory:\nDJANGO_ROOT = dirname(dirname(abspath(__file__)))\n# Absolute filesystem path to the top-level project folder:\nSITE_ROOT = dirname(DJANGO_ROOT)\n\nSTATIC_URL = '/static/'\nSTATIC_ROOT = normpath(join(SITE_ROOT, 'static_root'))\n",
2015-03-10T10:39:41
phanhuy152 :

I have been dealing with the same problem too. And here are the 2 things that I changed in my code.\n\n(I'm using Django 1.7)\n\n1) settings.py\n\nI add these lines to the setting files\n\nBASE_DIR = os.path.dirname(os.path.dirname(__file__))\nSTATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')\nSTATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'\nTEMPLATE_DIRS = (\n os.path.join(BASE_DIR, 'templates'),\n # Add to this list all the locations containing your static files \n)\n\n\nSTATIC_ROOT: this tells Django where to (a) put the static files when you run python manage.py collectstatic and (b) find the static files when you run the application\n\nTEMPLATE_DIRS: this tells Django where to look for your static files when it search for statics files when you run python manage.py collectstatic\n\n2) wsgi.py\n\nOriginally my file was:\n\nimport os\nos.environ.setdefault(\"DJANGO_SETTINGS_MODULE\", \"xxxx.settings\")\n\nfrom django.core.wsgi import get_wsgi_application\napplication = get_wsgi_application()\n\n\nAnd I changed it to:\n\nimport os\nos.environ.setdefault(\"DJANGO_SETTINGS_MODULE\", \"xxxx.settings\")\n\nfrom django.core.wsgi import get_wsgi_application\nfrom whitenoise.django import DjangoWhiteNoise\napplication = get_wsgi_application()\napplication = DjangoWhiteNoise(application)\n\n\nRead here for more information on whitenoise: https://devcenter.heroku.com/articles/django-assets#whitenoise\n\n\n\nAlso, remember to install whitenoise:\n pip install whitenoise==2.0.6\n\nBefore deploying the project, run:\n python manage.py collectstatic\n\nThis will create a folder indicated by STATIC_ROOT (declared in your settings.py), containing all your static files.",
2016-06-30T15:46:24
raspberry_next :

Since it's been a few years since this was posted (and it still pops up when I search for the issue), here's what worked for me in Django 3.2.\npip install whitenoise\n\nMake sure in settings.py you have\nSTATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')\nSTATIC_URL = '/static/'\n\n# Extra places for collectstatic to find static files.\nSTATICFILES_DIRS = (\n os.path.join(BASE_DIR, 'static'),\n)\n\nAdd whitenoise to your Middleware:\nMIDDLEWARE = [\n 'whitenoise.middleware.WhiteNoiseMiddleware',\n]\n\nMake sure the directory you specified as the STATIC_ROOT (staticfiles) exists in your base directory and is not empty.\nAfter that, I committed the changes and Heroku was able to build the static files and everything worked fine.",
2021-04-14T20:58:27
yy