12944qwerty
12944qwerty

Reputation: 2007

Deploying my Django website to Heroku with DEBUG=False doesn't work

When deploying django onto either localhost or heroku with DEBUG=False, it throws an error saying

C:\Users\krish\Envs\PRREMIA\lib\site-packages\whitenoise\base.py:105: UserWarning: No directory at: c:\Users\krish\Documents\python\PRREMIA\staticfiles\
  warnings.warn(u'No directory at: {}'.format(root))
[28/Jul/2019 16:05:43] "GET / HTTP/1.1" 500 27

When DEBUG=True, it works fine.

STATIC SETTINGS

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

On Github

Why? And how do I stop and fix this?

Note: Removing whitenose middleware from MIDDLEWARE and changing STATICFILES_STORAGE

# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'

removed the 500 Error, but the staticfiles are still not found.

Upvotes: 1

Views: 2698

Answers (2)

skitzCoder
skitzCoder

Reputation: 11

Configure Django / Create-React-App / Whitenoise to run git deploy to Heroku App with DEBUG = False

Big thank-you to Whitenoise developers first off to make this all possible!

In your settings.py

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'staticfiles')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },

    },
]

STATIC_URL = '/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'build/static'),     os.path.join(BASE_DIR, 'build')
]

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

STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

In views.py create a template view of index.html

from django.views.generic import TemplateView
from django.views.decorators.cache import never_cache

# Serve Single Page Application
index = never_cache(TemplateView.as_view(template_name='index.html'))

In urls.py add path to your new view

from django.urls import path, include, re_path
from .views import index

urlpatterns += [
    re_path('.*', index)
]

Run npm run build and then python manage.py collectstatic before pushing to heroku. This will build your application into a build folder, and then collect these static build files into a staticfiles folder in your project root.

If python manage.py collectstatic fails, create an empty directory called staticfiles in the project root first.

The point of the index re_path view is to always point your application back to index.html when reloading a page at a path like '/login'.

git add .
git commit -m "blah"
git push heroku master

This worked for me, hopefully it helps people somewhat in the future.

If this does not work, my advice is to investigate by running python manage.py runserver locally and messing around with your build folder, staticfiles folder, and settings.py to figure out what is going on. I would also recommend logging to find the error, although I did not do this myself.

Upvotes: 1

D. Evans
D. Evans

Reputation: 3292

Your STATICFILES_DIRS setting is incorrect:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

This refers to a directory called static in the root of your project, but there is no such directory.

It looks like your static files are inside the business application, in which case they will be picked up automatically so you can just remove the STATICFILES_DIRS setting altogether.

Upvotes: 0

Related Questions