Search code examples
djangoherokudjango-staticfilesdjango-settingsstatic-files

Django static files not coming through when deployed to Heroku


My static files are not coming through when I am deploying to Heroku. It works fine when I running on my local.

Its not a 404 error, on the network tab it seems like its not even requesting the static files at all.

I have followed this solution on Heroku's website: https://devcenter.heroku.com/articles/django-assets but still not working.

I looked at other solutions and I am already using WhiteNoise.

Here is the relevant code from my settings.py

import os
import environ
import django_heroku 


env = environ.Env(
    # set casting, default value
    DEBUG=(bool, False)
)

environ.Env.read_env()


BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))


# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = (env('DEBUG_VALUE') == "True")

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    #my own apps
    'corsheaders',
    'rest_framework',
    'paintings',
    'storages',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

CORS_ORIGIN_ALLOW_ALL = True

ROOT_URLCONF = 't_and_b_website.urls'


WSGI_APPLICATION = 't_and_b_website.wsgi.application'


STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'staticfiles')

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'staticfiles'), 
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

#MEDIA_ROOT is for directory for any media that our users upload
MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'media_cdn')
MEDIA_URL = '/media_cdn/'


CORS_URLS_REGEX = r'^/api.*'
CORS_ORIGIN_ALLOW_ALL = True
CORS_ORIGIN_WHITELIST = (
    '*',
    'your-domain.com',
    'your-bucket-here.s3-us-west-2.amazonaws.com',
)


AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')


AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None


DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Here is my wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "t_and_b_website.settings")

application = get_wsgi_application()

In my Heroku logs gives me this warning, so I am not sure if its picking up my Heroku environment variables:

/app/.heroku/python/lib/python3.6/site-packages/environ/environ.py:630: 
UserWarning: /app/src/t_and_b_website/.env doesn't exist - 
if you're not configuring your environment separately, create one.

Thanks so much in advance!


Solution

  • Sorry guys - stupid mistake on my part. Turns out the problem was I gitignored my own staticfiles folder.