简体   繁体   English

如何运行 django-admin check 命令?

[英]How to run the django-admin check command?

I just installed Django and am trying to run it for the first time .我刚刚安装了 Django 并且正在尝试第一次运行它。

For now, all I did is pip3 install Django and django-admin startproject现在,我所做的只是pip3 install Djangodjango-admin startproject

Right after installing it, I ran manage.py check and django-admin check .安装后,我立即运行manage.py checkdjango-admin check manage.py check displayed no issues, but django-admin check output was : manage.py check显示没有问题,但django-admin check输出是:

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我知道我必须编辑DJANGO_SETTINGS_MODULE环境变量,但是

  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.运行django-admin check --settings=djangotest.settings解决了django.core.exceptions.ImproperlyConfigured错误。

Now I have a new ModuleNotFoundError: No module named 'djangotest' error.现在我有一个新的ModuleNotFoundError: No module named 'djangotest'错误。 Indeed, djangotest is the name of my project.确实, djangotest是我的项目的名称。 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 ?为什么 django-admin 找不到这个文件,我该如何解决?

Django needs to know where your settings are when it runs commands. Django 在运行命令时需要知道您的设置在哪里。 When using manage.py commands each project sets it's settings path to the manage.py file so it knows where to look.当使用manage.py命令时,每个项目都会将它的设置路径设置为manage.py文件,以便它知道在哪里查找。

django-admin is Django's tool for administrative tasks at a system level, not specific to a given project. django-admin是 Django 的系统级管理任务工具,并非特定于给定项目。 Therefore, setting the DJANGO_SETTINGS_MODULE or passing the settings option to django-admin is generally required when running commands with it.因此,在使用它运行命令时,通常需要设置DJANGO_SETTINGS_MODULE或将settings选项传递给django-admin

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.因此,并不是您的设置有任何问题,只是django-admin不知道某个项目,因为它的范围在系统级别。 Therefore you need to point it in the correct direction of a project in order to run a check .因此,您需要将其指向项目的正确方向才能运行check

By default startproject will have created a settings.py file in a module named after the project name you chose.默认情况下, startproject将在以您选择的项目名称命名的模块中创建一个settings.py文件。

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;因此,基于此,您可以使用值mysite.settings设置DJANGO_SETTINGS_MODULE的环境变量,例如,您的项目所在的位置;

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

Alternatively you can pass your settings path to commands, eg或者,您可以将设置路径传递给命令,例如

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

django-admin is a system tool to manage any django project. django-admin是一个管理任何 django 项目的系统工具。 By default it doesn't know which one.默认情况下,它不知道是哪一个。

If you run django-admin check you will get:如果你运行django-admin check你会得到:

django.core.exceptions.ImproperlyConfigured: Requested setting ROOT_URLCONF, but settings are not configured. django.core.exceptions.ImproperlyConfigured:请求设置 ROOT_URLCONF,但未配置设置。 You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.在访问设置之前,您必须定义环境变量 DJANGO_SETTINGS_MODULE 或调用 settings.configure()。

because django-admin doesn't know which project settings files to use.因为django-admin不知道要使用哪个项目设置文件。

If you run django-admin check --settings=djangotest.settings you will get:如果你运行django-admin check --settings=djangotest.settings你会得到:

ModuleNotFoundError: No module named 'djangotest' ModuleNotFoundError: 没有名为“djangotest”的模块

because now django knows what to use but python cannot find a module named djangotest on sys.path .因为现在 django 知道要使用什么,但是 python 在sys.path上找不到名为djangotest的模块。

You have to add project directory / current directory to the PYTHONPATH env variable:您必须将项目目录/当前目录添加到PYTHONPATH变量:

PYTHONPATH="$(pwd)"
django-admin check --settings=djangotest.settings

or specify it as an argument:或将其指定为参数:

django-admin check --settings=djangotest.settings --pythonpath=.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM