django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded

121.1k views Asked by At

The scenario is,

I cloned the Django code for OpenShift-V3 from here . When I ran the code with python manage.py runserver, I got this error:

django.core.exceptions.ImproperlyConfigured: WSGI application 'application' could not be loaded; Error importing module: 'application doesn't look like a module path

I didn't add anything to the code and the required packages are already satisfied.

30

There are 30 answers

1
AudioBubble On BEST ANSWER

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 :(

0
Naazneen Jatu On

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
prarthana wickramarathne On

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....

0
Frig Mortu On

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
)
1
Mahdi Kh On

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
mansoor.khan On

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
Ravindranath Avvaru On

make sure your app name written in

INSTALLED_APPS = [ 
...
'App_name', 
]
0
Tejaswita Som On

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.

0
unicdev On

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
daggett On

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.

0
Jakob On

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.

0
Ravi Ranjan On

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
JohnShallow On

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
Ramin Azali On

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
Faizun Faria On

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.

0
Shah Vipul On

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

0
Yalchin Mammadli On

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
Akorede Habeebullah On

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

4
Vladimir On

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

1
Hasan On

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.

0
Abhishek Patil On

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.

0
vinchuli On

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
MrE On

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.

0
Sazib On

It's working for me

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

My CORS Configuration

ALLOWED_HOSTS=['*']
CROS_ORIGIN_ALLOW_ALL = True
2
Ömer ÇELEBİ On

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'
1
B.Hatuwal On

in settings.py file change as follows:

WSGI_APPLICATION = 'your_project_name.wsgi.application'

0
M3RS On

For Django 4.2 / Django 5.0 in settings.py I had to change

WSGI_APPLICATION = 'ubereats_tracker.wsgi.app'

to

WSGI_APPLICATION = 'ubereats_tracker.wsgi.application'
0
TadFoin On

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',
    ...
]
0
Christopher Nolan On
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.

0
Ngatia Frankline On

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',
    ...
]