2

I'm getting this error whenever I'm trying to run python manage.py runserver in Django.

The error:

django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

Why can that be?

I think it is because my settings.py it is called base.py instead of settings.py and that's why Django doesn't detect that file as my settings file. If that's the case, how can I assign the settigns file to that base.py fiile?

this is the base.py file:

import os
from pathlib import Path

from django.urls import reverse_lazy

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

########################################
# APPS
########################################
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.humanize',
    'allauth',
    'allauth.account',
    'djstripe',
    'captcha',
    'users',
]

########################################
# MIDDLEWARE
########################################
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',
]

########################################
# SECURITY
########################################
SECRET_KEY = os.environ.get("SECRET_KEY")
DEBUG = bool(os.getenv("DJANGO_DEBUG", "False").lower() in ["true", "1"])
ALLOWED_HOSTS = []

########################################
# OTHER
########################################
ROOT_URLCONF = 'urls'
WSGI_APPLICATION = 'wsgi.application'
SITE_ID = 1

########################################
# TEMPLATES
########################################
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR / "templates",
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'context_processors.plan_context',
                'context_processors.base_url_context',
                'context_processors.stripe_context',
            ],
        },
    },
]

########################################
# DATABASE
########################################
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.environ.get("POSTGRES_DB"),
        'USER': os.environ.get("POSTGRES_USER"),
        'PASSWORD': os.environ.get("POSTGRES_PASSWORD"),
        'HOST': 'postgres',
        'PORT': '5432',
    }
}

########################################
# PASSWORD VALIDATION
########################################
AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

########################################
# INTERNATIONALISATION
########################################
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True

########################################
# STATIC SETTINGS
########################################
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    BASE_DIR / "static"
]

########################################
# AUTHENTICATION
########################################
AUTH_USER_MODEL = 'users.User'
AUTHENTICATION_BACKENDS = (
    "allauth.account.auth_backends.AuthenticationBackend",
)
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_VERIFICATION = 'optional'
ACCOUNT_LOGOUT_ON_GET = True
ACCOUNT_SIGNUP_PASSWORD_ENTER_TWICE = True
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
ACCOUNT_FORMS = {'signup': 'users.forms.CustomSignupForm'}
ACCOUNT_EMAIL_SUBJECT_PREFIX = ""
LOGIN_REDIRECT_URL = reverse_lazy("users:dashboard")
LOGIN_URL = reverse_lazy("account_login")

########################################
# SITE SETTINGS
########################################
# todo: set your site name, your country and your support email here
SITE_NAME = "launchr"
SITE_LOCATION = "United States"
SUPPORT_EMAIL = "support@example.com"
godtrianglefd
  • 179
  • 2
  • 12
  • nope, now it shows ```django.core.exceptions.ImproperlyConfigured: The SECRET_KEY setting must not be empty.``` but i've already assigned a key to it. – godtrianglefd Feb 10 '21 at 10:17
  • Your `SECRET_KEY` is empty, give the default value: `SECRET_KEY = os.environ.get('SECRET_KEY', 'default_key')`. – NKSM Feb 10 '21 at 11:59
  • Does this answer your question? [ImproperlyConfigured: You must either define the environment variable DJANGO\_SETTINGS\_MODULE or call settings.configure() before accessing settings](https://stackoverflow.com/questions/26082128/improperlyconfigured-you-must-either-define-the-environment-variable-django-set) – Stephen C Jul 30 '22 at 04:22

1 Answers1

1

You can assign your settings file in manage.py:

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

manage.py:

#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault('DJANGO_SETTINGS_MODULE',
                          'djangoproject.settings.dev')

    from django.core.management import execute_from_command_line
    execute_from_command_line(sys.argv)

Or you can use the --settings command-line argument to specify the settings manually:

python manage.py runserver --settings=mysite.settings

See DJANGO_SETTINGS_MODULE if you need more info.

NKSM
  • 5,422
  • 4
  • 25
  • 38