Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is whitenoise crashing in a default Django project on Heroku?

I'm trying to launch a new Django app, following Heroku's instructions. When I get to activating whitenoise it crashes. I haven't even written any app code yet. What's going wrong? Here's the traceback of a web worker crashing:

[2015-06-26 20:37:36 +0000] [11] [ERROR] Exception in worker process:
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/arbiter.py", line 507, in spawn_worker
    worker.init_process()
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/workers/base.py", line 118, in init_process
    self.wsgi = self.app.wsgi()
    self.callable = self.load()
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 65, in load
    return self.load_wsgiapp()
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/app/.heroku/python/lib/python2.7/site-packages/gunicorn/util.py", line 355, in import_app
    __import__(module)
  File "/app/prompt/wsgi.py", line 4, in <module>
    from whitenoise.django import DjangoWhiteNoise
  File "/app/.heroku/python/lib/python2.7/site-packages/whitenoise/django.py", line 14, in <module>
    from django.contrib.staticfiles.storage import staticfiles_storage
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/storage.py", line 12, in <module>
    from django.core.cache import (
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/cache/__init__.py", line 34, in <module>
    if DEFAULT_CACHE_ALIAS not in settings.CACHES:
  File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
  File "/app/.heroku/python/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._setup(name)
    % (desc, ENVIRONMENT_VARIABLE))
ImproperlyConfigured: Requested setting CACHES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
    worker.init_process()

Note that the application worked fine, correctly serving the index page 404, until I edited the wsgi.py file as specified in https://devcenter.heroku.com/articles/django-assets

like image 813
Andrew B. Avatar asked Dec 15 '22 13:12

Andrew B.


1 Answers

What does your wsgi.py file look like? Note that you must initialize settings before importing whitenoise, as per this issue on github. Try something like this:

import os

from django.core.wsgi import get_wsgi_application

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

from whitenoise.django import DjangoWhiteNoise

application = DjangoWhiteNoise(get_wsgi_application())
like image 138
Jessamyn Smith Avatar answered Dec 17 '22 01:12

Jessamyn Smith