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

房东的猫 提交于 2020-06-29 04:53:06

问题


I can't understand why I'm facing an error while running Heroku run python manage.py runserver

I tried changing folder names and I tried removing and adding WhiteNoise in MIDDLEWARE

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

database settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

django_heroku.settings(locals())

wsgi.py

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)

at least the server should run so that I can see whats the issue with Heroku.


回答1:


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.




回答2:


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')



来源:https://stackoverflow.com/questions/56621436/django-core-exceptions-improperlyconfigured-wsgi-application-project-name-ws

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!