[Django]-Django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded

17👍

Go to django-ex/project/settings.py

Change the line in settings.py as below

WSGI_APPLICATION = 'application' to WSGI_APPLICATION = 'wsgi.application'

That’s it 🙁

48👍

I used a middleware CorsMiddleware but forget to install it so after install, it works perfectly.

pip install django-cors-headers

So check something like it you may miss something like it.

👤Hasan

30👍

Read carefully, it might say “The above exception was the direct cause of the following exception: …”. And the “above exception” being you forgot to install whitehoise. Run pip install whitenoise, it worked for me.

16👍

If you run django project locally for development, just remove WSGI_APPLICATION variable from settings.py module. It needs in prod/stage settings, for example settings_prod.py

7👍

Do you have Django Debug Toolbar

Remove it and check if the problem goes away. Possible occurences:

pip uninstall django-debug-toolbar

INSTALLED_APPS = [
    ...
    'debug_toolbar',
    ...
]

MIDDLEWARE = [
    ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    ...
]

6👍

This worked for me

Install this if you didn’t before:

pip install whitenoise

in settings.py add :

MIDDLEWARE = [
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

5👍

pip install whitenoise

Although solved my problem. Generally produced while we are moving a project to different virtual enviroments. Sometimes we forgot to install the package & whitenoise just broke the application little bit, because no where it is mentioned that you are missing "whitenoise" module.

3👍

in settings.py file
change as follows:

WSGI_APPLICATION = ‘your_project_name.wsgi.application’

3👍

note that any error in importing modules anywhere prior to starting the wsgi application will also prompt this message, so first look at the trace and start from the top in fixing issues.

I ported a Django app from python 2.7 to python3 and add all sorts of module import issues, not connected to this issue directly.

👤MrE

3👍

Check your middleware and delete error string

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',
]
raise ImportError(
    ImportError: Module "django.middleware.locale" does not define a "LocaleMiddlewarez" attribute/class
)

2👍

I was using django-cors-headers, then I thought I haven’t implemented cors in my project, so went ahead to install django-cors-middleware, then it’s started giving the wsgi exception, so i checked the stack trace and I found out that it’s django-cors-headers and django-cors-middleware conflicting each other. I had to uninstall django-cors-middleware but it’s still gives the same exception, so uninstall django-cors-headers too then reinstall and everything works fine….

2👍

Same problem..
I checked whether django-cors-middleware and django-cors-headers installed or not. I found those were not installed and then I installed them.

  1. python -m pip install django-cors-middleware
  2. python -m pip install django-cors-headers

Then,

  1. python manage.py migrate
  2. python manage.py runserver

Finally, it’s worked….

2👍

Go to settings.py :

  • In MIDDLEWARE, check whether you have added anything which is not working.
  • In INSTALLED_APPS, check whether you have added the apps or not. If not, add all the apps you have created for this project.

Example: I had this error today and was totally unaware why is this happening then after checking for a while, I have found a very silly mistake e.g. I have added my newly created app in the MIDDLEWARE instead of INSTALLED_APPS.

1👍

In the latest version of Django, to add your app in installed apps in settings.py file, write –

[
….,
<appname>.apps.<Appname>Config‘,
]

where <Appname>Config is the name of the function in the apps.py file.

1👍

You may not have entered your module name correctly in the setting.py or you may have added it incorrectly in wrong place like middleware…

0👍

I tried all these solution and the one worked for me was this:

pip install django-htmlmin

This module was missing and not mentioned in requirements.txt
Shows long error message that ends with wsgi application improperly configured. But somewhere in middle I could see "No module named 'htmlmin' I installed and it is resolved.

0👍

maybe using middleware is the simple way to fix this , this problem mostly happens about cors Policy

Code:

class CorsMiddleware(object):
   def __init__(self, get_response):
      self.get_response = get_response

   def __call__(self, request):
      return self.get_response(request)

   def process_response(self,resp):
      resp["Access-Control-Allow-Origin"] = "*"
      return resp

0👍

It happens when you insert something in MIDDLEWARE = [……]
solution tire to put added latest addend things at top of middleware.

0👍

You may also get this error while adding your custom middleware. While adding it, make sure you so it like this:
folder_contains_your_middleware_file.middleware_file.middleware_class

Below is my example:

'company_management.loginCheckMiddleware.LoginCheckMiddleware'

0👍

I also had the same problem, in spite of trying all the above solutions I was facing the same issue.
I noticed that before this error I was getting the below error:

ModuleNotFoundError: No module named 'log_request_id'

To resolve this error I installed the django-log-request-id package using pip install django-log-request-id and both the issues got resolved.

0👍

I removed the custom middleware, but forgot to exclude it from MIDDLEWARES in the settings.py module, so be sure to verify that as well.

0👍

Inspecting the logs, the same error was thrown due to a "WhitenoiseMiddleware" error, such as this.

The way to solve it was to make sure that WhiteNoiseMiddleware is placed directly after the Django SecurityMiddleware (if you are using it) and before all other middleware.
In settings.py:

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',  # <-- here
  # ...
]

Check the Django documentation for details.

👤Jakob

0👍

It’s working for me

 MIDDLEWARE = [
        'corsheaders.middleware.CorsMiddleware', # add only comma 
    ]

My CORS Configuration

ALLOWED_HOSTS=['*']
CROS_ORIGIN_ALLOW_ALL = True
👤Sazib

0👍

As it turned out, in my case, the project dependency: django-request-logging was not installed. This was resulting in the same error

pipenv install

installed the dependency and fixed the issue for me.

0👍

make sure your app name written in

INSTALLED_APPS = [ 
...
'App_name', 
]

0👍

This is caused by a number of issues. Just confirm:

  1. If you are using gunicorn to server you django app ensure Whitenose is installed,and added to your middlewares and your gunicorn command lookes something like gunicorn APP-NAME.wsgi:application --bind 0.0.0.0:8000 --timeout 120 --workers 2 --log-level info and ensure your settings.py has something likeWSGI_APPLICATION = "APP-NAME.wsgi.application"
  2. If above is correct or you ain`t using gunicorn. Kindly check your middlewares. Most of the time you might have a middleware thats not installed that maybe blocking the server from starting.

0👍

Try removing ‘django.contrib.auth.middleware.AuthenticationMiddleware’ from MIDDLEWARE. This worked for me when I had similar issue.

0👍

I would be surprised if that’s the case but personally I had the same error and the problem was just a typo in the middlewares, I wrote corseheaders (with an extra "e") instead of corsheaders

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    ...
]

-1👍

Make sure you are in desired python Environment

Get the requirements.txt file or the python modules list, which are needed to execute django.

Install all the Modules and you shall be good to go.

Leave a comment