尝试创建灯具并在终端中运行下一个命令时出现此错误:
django-admin.py dumpdata data.json
完整追溯:
(project)litwisha@litwisha:~/PycharmProjects/test_project$ django-admin.py dumpdata data.json
Traceback (most recent call last):
File "/home/litwisha/.virtualenvs/project/bin/django-admin.py", line 5, in <module>
management.execute_from_command_line()
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/core/management/base.py", line 402, in run_from_argv
connections.close_all()
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/utils.py", line 258, in close_all
for alias in self:
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/utils.py", line 252, in __iter__
return iter(self.databases)
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/utils/functional.py", line 60, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/db/utils.py", line 151, in databases
self._databases = settings.DATABASES
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in __getattr__
self._setup(name)
File "/home/litwisha/.virtualenvs/project/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, 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.
尝试了此solution,但无效:
在shell中写作时:
from django.conf import settings
settings.configure()
得到错误:
RuntimeError: Settings already configured.
也许是因为我使用virtualenv
?但是我在没有virtualenv
的情况下尝试了它,但它不起作用。我使用IDE PyCharm
settings.py:
"""
Django settings for test_project project.
Generated by 'django-admin startproject' using Django 1.8.3.
For more information on this file, see
https://docs.djangoproject.com/en/1.8/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.8/ref/settings/
"""
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
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.8/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'r1n@)rahbrh6t1hb-mn_83c^7ai@ij&7d8m8mo7tdm&5q#o5t&'
# 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',
'app',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
)
ROOT_URLCONF = 'test_project.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 = 'test_project.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'test1',
'USER': 'postgres',
'PASSWORD': '1111',
'HOST': '127.0.0.1',
'PORT': '5432'
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.8/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.8/howto/static-files/
STATIC_URL = '/static/'