Django数据库设置“错误配置”错误 [英] Django DB Settings 'Improperly Configured' Error
问题描述
。 contrib.auth.models import User
- 追溯(最近的最后一次呼叫):
文件/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py,第36行,在_setup
settings_module = os.environ [ENVIRONMENT_VARIABLE]
文件 /usr/lib/python3.2/os.py,第450行,__getitem__
value = self._data [self.encodekey(key)]
KeyError:b'DJANGO_SETTINGS_MODULE'
在处理上述异常时,发生另一个异常:
追溯(最近的最后一次调用):
文件< stdin>,第1行在< module> ;
文件/usr/local/lib/python3.2/dist-packages/django/contrib/auth/models.py,第8行在< module>
从django.db导入模型
文件/usr/local/lib/python3.2/dist-packages/django/db/__init__.py,第11行在< module>
如果settings.DATABASES和DEFAULT_DB_ALIAS不在设置中.DATABASES:
文件/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py,第52行, __getattr__
self._setup(name)
文件/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py,第45行,_setup
%(desc,ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured:请求的设置DATABASES,
但设置未配置。您必须在访问设置之前定义环境
变量DJANGO_SETTINGS_MODULE或调用settings.configure()
。
如果在Python解释器之外工作得很好,怎么配置不正确?在我的Django设置中, DATABASES
设置为:
DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql_psycopg2',#添加'postgresql_psycopg2','mysql','sqlite3'或'oracle'。
'NAME':'django_db',#或使用sqlite3的数据库文件路径。
#sqlite3不使用以下设置:
'USER':'zamphatta',
'PASSWORD':'mypassword91',
'HOST':'',通过域套接字为本地主机为空,或通过TCP为本地主机为127.0.0.1。
'PORT':'',#设置为空字符串为默认值。
}
}
...这是不正确配置的? p>
你不能只是启动Python并检查事情,Django不知道你想要工作的项目。您必须执行以下其中一项操作:
- 使用
python manage.py shell
- 使用
django-admin.py shell --settings = mysite.settings
(或您使用的任何设置模块) - 将您的操作系统中的
DJANGO_SETTINGS_MODULE
环境变量设置为mysite.settings
-
(在Django 1.6中删除)在python解释器中使用
setup_environ
从django.core.management导入setup_environ
/ pre>
从mysite导入设置
setup_environ(设置)
自然,第一种方法是最简单的。
Django (1.5) is workin' fine for me, but when I fire up the Python interpreter (Python 3) to check some things, I get the weirdest error when I try importing - from django.contrib.auth.models import User
-
Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 36, in _setup
settings_module = os.environ[ENVIRONMENT_VARIABLE]
File "/usr/lib/python3.2/os.py", line 450, in __getitem__
value = self._data[self.encodekey(key)]
KeyError: b'DJANGO_SETTINGS_MODULE'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/dist-packages/django/contrib/auth/models.py", line 8, in <module>
from django.db import models
File "/usr/local/lib/python3.2/dist-packages/django/db/__init__.py", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 52, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.2/dist-packages/django/conf/__init__.py", line 45, 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.
How could it be improperly configured, when it works fine outside the Python interpreter? In my Django settings, the DATABASES
settings are:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'zamphatta',
'PASSWORD': 'mypassword91',
'HOST': '', # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
'PORT': '', # Set to empty string for default.
}
}
...how is this improperly configured?
You can't just fire up Python and check things, Django doesn't know what project you want to work on. You have to do one of these things:
- Use
python manage.py shell
- Use
django-admin.py shell --settings=mysite.settings
(or whatever settings module you use) - Set
DJANGO_SETTINGS_MODULE
environment variable in your OS tomysite.settings
(This is removed in Django 1.6) Use
setup_environ
in the python interpreter:from django.core.management import setup_environ from mysite import settings setup_environ(settings)
Naturally, the first way is the easiest.
这篇关于Django数据库设置“错误配置”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!