13

I have an almost fresh install of django and when I run python manage.py runserver. It is giving me this error:

ImproperlyConfigured: WSGI application 'myproject.wsgi.application' could not be loaded; Error importing module.

settings.py

WSGI_APPLICATION = 'myproject.wsgi.application'

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
application = get_wsgi_application()
tommyp
  • 331
  • 1
  • 3
  • 12

10 Answers10

18

Comment out the

#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',

in your settings.py file in Middleware

skuli434
  • 181
  • 1
  • 4
  • 11
    It's better to remove this line, not comment out the code. Also some explanation would be helpful, since this is caused by deleting SessionAuthenticationMiddleware in django 2.0 (which was deprecated earlier in django 1.10). – gonczor Jul 09 '18 at 08:51
11

From my experience this happens when I try to execute runserver but I haven't installed all custom MIDDLEWARE in setting.py. After identifying and installing the middlewares the error is resolved.

nixenerlan
  • 126
  • 1
  • 5
7

Check the settings.py,

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

remove 'whitenoise.middleware.WhiteNoiseMiddleware', or install Whitenoise (pip install whitenoise)

tnductam
  • 397
  • 4
  • 10
  • Whitenoise was the problem also for me, I've copied the settings.py from another projects where I did use whitenoise, had to deleted for this one. ty – Omar Gonzales Jun 13 '19 at 20:22
4

Check for the stack trace - you might find answer few lines above the line "The above exception was the direct cause of the following exception:"

It may caused for example by using of middleware from some uninstalled third party app etc.

Ondra
  • 943
  • 12
  • 19
3

One of the reason for this issue is if you have a middleware added into django middleware list in settings.py but haven't installed it yet.

In my case I added corsheaders.middleware.CorsMiddleware which wasn't installed.I installed it using pip install django-cors-headers and that did the trick.

monofal
  • 1,928
  • 1
  • 13
  • 15
1

I encountered the same problem because I added the debug_toolbar Middleware to my settings.py

'debug_toolbar.middleware.DebugToolbarMiddleware',

I solved the problem by removing the debug_toolbar Middleware. I also had to remove debug_toolbar from my INSTALLED APPS.

ArturoB
  • 39
  • 3
1

For whitenoise version 4.0 or above: - 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.

MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]
  • The 'whitenoise.django.GzipManifestStaticFilesStorage' alias has now been removed. Instead you should use the correct import path: 'whitenoise.storage.CompressedManifestStaticFilesStorage'.
Siddharth Das
  • 1,057
  • 1
  • 15
  • 33
1

for anyone having this same issue. i just fixed it following the instructions here

you should add WhiteNoise to your middleware list in settings.py and remove any reference to WhiteNoise from wsgi.py.

Romain BOBOE
  • 367
  • 3
  • 10
0

I had the same error, and in my case, I had made a custom middleware and then added it to the middleware list in settings.py under MIDDLEWARE. The problem was in the casing that I had used for listing the middleware in the list. I changed it to match the casing of my custom one and it worked!

sajeyks mwangi
  • 549
  • 8
  • 20
0

got the same error when mistakenly registering a app in MIDDLEWARE = [] insted of INSTALLED_APPS = []

benAv
  • 35
  • 5