Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django设置无法正常工作_Python_Django_Python 3.5 - Fatal编程技术网

Python Django设置无法正常工作

Python Django设置无法正常工作,python,django,python-3.5,Python,Django,Python 3.5,我能执行 python manage.py migrate - it executes perfectly 但是当我跑的时候 django-admin shell 它无法给出以下错误 Traceback (most recent call last): File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv

我能执行

python manage.py migrate - it executes perfectly
但是当我跑的时候

django-admin shell
它无法给出以下错误

Traceback (most recent call last):
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/base.py", line 294, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/base.py", line 337, in execute
    saved_locale = translation.get_language()
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/utils/translation/__init__.py", line 190, in get_language
    return _trans.get_language()
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/utils/translation/__init__.py", line 57, in __getattr__
    if settings.USE_I18N:
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting USE_I18N, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/cj/.vtenv/officingx/bin/django-admin", line 11, in <module>
    sys.exit(execute_from_command_line())
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/core/management/base.py", line 306, in run_from_argv
    connections.close_all()
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/db/utils.py", line 229, in close_all
    for alias in self:
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/db/utils.py", line 223, in __iter__
    return iter(self.databases)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/db/utils.py", line 156, in databases
    self._databases = settings.DATABASES
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/home/cj/.vtenv/officingx/lib/python3.5/site-packages/django/conf/__init__.py", line 39, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
请帮助我无法理解为什么即使migrate命令工作正常,它也无法读取设置

取自:

通常,在处理单个Django项目时,使用manage.py比使用Django admin更容易。如果需要在多个Django设置文件之间切换,请使用Django admin和Django_settings_模块或--settings命令行选项

您没有在
django admin
中指定模块(作为参数),这就是django抱怨的原因。这样做:
django admin shell--settings=myproject.settings

此外,从:

设置模块应采用Python包语法,例如mysite.settings。如果没有提供,django admin将使用django_SETTINGS_MODULE环境变量

您还可以执行以下操作:

通常,在处理单个Django项目时,使用manage.py比使用Django admin更容易。如果需要在多个Django设置文件之间切换,请使用Django admin和Django_settings_模块或--settings命令行选项

您没有在
django admin
中指定模块(作为参数),这就是django抱怨的原因。这样做:
django admin shell--settings=myproject.settings

此外,从:

设置模块应采用Python包语法,例如mysite.settings。如果没有提供,django admin将使用django_SETTINGS_MODULE环境变量


您还可以使用
python manage.py shell
python manage.py runserver
执行
/manage.py shell

。在
manage.py
文件中,您可以看到它如何设置
DJANGO\u设置\u模块
变量。如果要使用django管理系统命令,则需要导出环境变量(
export-django\u SETTINGS\u MODULE=“manage.py文件中的设置文件路径”
你看不到你在使用另一个命令吗?你应该坚持使用
manage.py
这两个命令。@chirag7jain做了下面的回答帮助你解决了你的问题吗?使用
python manage.py shell
python manage.py runserver
。在
manage.py
文件中,你可以看到它是如何设置的LE
变量。如果要使用django管理系统命令,则需要导出环境变量(
export django\u SETTINGS\u MODULE=“manage.py文件中的设置文件路径”
你看不到你在使用另一个命令吗?你应该坚持使用
manage.py
这两个命令。@chirag7jain下面的答案是否帮助你解决了问题?
"""
Django settings for officingx project.

Generated by 'django-admin startproject' using Django 1.10.5.

For more information on this file, see
https://docs.djangoproject.com/en/1.10/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.10/ref/settings/
"""

import os

import dj_database_url

# 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/1.10/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ['OFFICINGX_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',

    'amenity.apps.AmenityConfig',
    'booking.apps.BookingConfig',
    'location.apps.LocationConfig',
    'organization.apps.OrganizationConfig',
    'photo.apps.PhotoConfig',
    'product.apps.ProductConfig',
    'property.apps.PropertyConfig',
]

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 = 'officingx.urls'

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

WSGI_APPLICATION = 'officingx.wsgi.application'


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

DATABASES = {'default': dj_database_url.config()}


# Password validation
# https://docs.djangoproject.com/en/1.10/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/1.10/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/1.10/howto/static-files/

STATIC_URL = '/static/'