You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地运行Python程序遇django.core.exceptions.ImproperlyConfigured错误求助

Django WSGI Application 'mysite.wsgi.application' Failed to Load (Python 3.7, Django 2.2)

Let’s walk through the likely issues and fixes for your error—this is a common snag when configuring Django for both local development and Heroku deployment.

Possible Solutions

1. Fix ALLOWED_HOSTS Format

Your current ALLOWED_HOSTS includes a full URL (https://stormy-stream-43261.herokuapp.com/) which is invalid. Django expects just the hostname without the protocol or trailing slash:

ALLOWED_HOSTS = ["localhost", "stormy-stream-43261.herokuapp.com"]

When DEBUG=False, Django enforces strict host validation, so this misconfiguration can trigger initialization failures.

2. Correct Your DATABASES Structure

Looking at your settings, your SQLite config has a formatting error. The ENGINE and NAME need to be nested under the default key, and using BASE_DIR ensures the database file path is correct for your project:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'mydatabase.sqlite3'),  # Uses project root for the DB file
    }
}

Your current setup places these keys directly in the DATABASES dictionary, which breaks Django’s required configuration structure and can lead to load errors.

3. Isolate django_heroku Configuration

The django_heroku.settings(locals()) line overwrites settings for Heroku’s environment, which can conflict with your local setup. Wrap it in a conditional to only run in production:

import os
# Only apply Heroku-specific settings in production
if os.environ.get('ENV') == 'production':
    django_heroku.settings(locals())

Or temporarily comment out this line when testing locally to rule out Heroku-related conflicts.

4. Verify Your wsgi.py File

Double-check that mysite/wsgi.py exists and has the standard content:

import os
from django.core.wsgi import get_wsgi_application

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

application = get_wsgi_application()

If this file is missing, corrupted, or in the wrong directory, Django can’t load the WSGI application.

5. Use DEBUG=True for Local Testing

While debugging locally, set DEBUG = True—this disables strict host checks and simplifies static file handling, making it easier to isolate the root issue:

DEBUG = True

You can flip this back to False when deploying to production.

6. Confirm Dependencies Are Installed

Make sure all required packages are installed and compatible with Django 2.2:

pip install django==2.2 django-heroku dj-database-url whitenoise

Missing or incompatible packages often cause import errors when loading the WSGI application.

After applying these changes, run python manage.py runserver again and see if the error clears up.

内容的提问来源于stack exchange,提问作者Thomas Zhen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:38:25