[Django]-Django.core.exceptions.ImproperlyConfigured: WSGI application '{project_name}.wsgi.application' could not be loaded; Error importing module

4πŸ‘

βœ…

As per documentation, middleware configuration should be like this:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

And you can remove the whitenoise related lines from wsgi file as well:

import os

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

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ss.settings')

application = get_wsgi_application()
#  application = DjangoWhiteNoise(application)

Because in whitenoise >= 4.0, you don’t need to change in wsgi file.

πŸ‘€ruddra

0πŸ‘

Your wsgi.py file is incorrect.

os.environ.setdefault('DJANGO_SETTINGS_MODULE', '{{PROJECT}}.settings')

The code above is the issue, you need to replace {{ project }} with your actual app name.

It’s the folder name where the settings.py is located.

For example, if this is your project structure.

blog
      ...
    - settings.py
    - wsgi.py
      ...

Then the correct code is os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'blog.settings')

πŸ‘€Kareem

Leave a comment