Django Error: 'myproject.wsgi.application' could not be loaded

user6335453 picture user6335453 · Dec 3, 2017 · Viewed 7.1k times · Source

I'm currently trying to test my Django application, which will eventually be deployed to Heroku. When testing locally, I'm running into the error:

django.core.exceptions.ImproperlyConfigured: WSGI application 
'foodForThought.wsgi.application' could not be loaded; Error importing module.

My wgsi.py file is configured as:

import os
import signal
import sys
import traceback
import time

from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise

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

application = get_wsgi_application()
application = DjangoWhiteNoise(application)

and my settings.py file is configured as:

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

...

# Application definition

INSTALLED_APPS = [
    'recipe.apps.RecipeConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'foodForThought'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    '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',
]

ROOT_URLCONF = 'foodForThought.urls'

...

WSGI_APPLICATION = 'foodForThought.wsgi.application'

...

I'm currently using Django 1.11.7 and testing in a venv. What's causing this issue to occur?

Answer

Mostafa Elgayar picture Mostafa Elgayar · Nov 5, 2018

This worked for me, I know I am 11 months late but I hope it works for anyone facing this problem. According to Whitenoise's changelog here. It says the following:

The WSGI integration option for Django (which involved editing wsgi.py) has been removed. Instead, you should add WhiteNoise to your middleware list in settings.py and remove any reference to WhiteNoise from wsgi.py. See the documentation for more details.

I did what they said, I removed any Whitenoise related lines from my wsgi.py file, and changed these 2 lines in MIDDLEWARE in my settings.py file:

'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',

and, this actually worked for me. Hope it works for you guys!