Search code examples
pythondjangopython-3.xenvironment-variablespython-venv

How to run the django-admin check command?


I just installed Django and am trying to run it for the first time.

For now, all I did is pip3 install Django and django-admin startproject

Right after installing it, I ran manage.py check and django-admin check. manage.py check displayed no issues, but django-admin check output was :

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

I understand I must edit the DJANGO_SETTINGS_MODULE environment variable, but

  1. What should its value be and when/how was it supposed to be set ?
  2. What did I possibly do wrong to get this Error ?

Update

running django-admin check --settings=djangotest.settings solved the django.core.exceptions.ImproperlyConfigured error.

Now I have a new ModuleNotFoundError: No module named 'djangotest' error. Indeed, djangotest is the name of my project. I checked and found the my settings file. It is as follows :

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '########################################' # not the real secret key

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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

ROOT_URLCONF = 'django_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        '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',
            ],
        },
    },
]

WSGI_APPLICATION = 'django_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

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


# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators

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',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/

STATIC_URL = '/static/'

Why can't django-admin find this file and how do I fix this ?


Solution

  • Django needs to know where your settings are when it runs commands. When using manage.py commands each project sets it's settings path to the manage.py file so it knows where to look.

    django-admin is Django's tool for administrative tasks at a system level, not specific to a given project. Therefore, setting the DJANGO_SETTINGS_MODULE or passing the settings option to django-admin is generally required when running commands with it.

    Therefore it's not that you have done anything wrong with your setup, it's just that django-admin doesn't know about a project because it's scope is at system level. Therefore you need to point it in the correct direction of a project in order to run a check.

    By default startproject will have created a settings.py file in a module named after the project name you chose.

    So based on that you could set an environment variable of DJANGO_SETTINGS_MODULE with the value mysite.settings, for example, where your project looks like;

    - project
    -- manage.py
    -- mysite
    --- __init__.py
    --- settings.py
    --- urls.py
    --- wsgi.py
    

    Alternatively you can pass your settings path to commands, e.g.

    django-admin check --settings=mysite.settings
    python manage.py check --settings=mysite.settings