Django DB 设置“配置不当”错误

Django (1.5)对我来说工作得很好,但是当我启动 Python 解释器(Python 3)来检查一些东西时,当我尝试导入-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.

当它在 Python 解释器之外工作得很好时,怎么可能配置不当呢?在我的 Django 设置中,DATABASES设置如下:

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.
}
}

这怎么会配置不当呢?

185379 次浏览

您不能只是启动 Python 并检查一些东西,Django 不知道您想从事什么项目。你必须做这些事情之一:

  • 使用 python manage.py shell
  • 使用 django-admin.py shell --settings=mysite.settings(或任何您使用的设置模块)
  • 将操作系统中的 DJANGO_SETTINGS_MODULE环境变量设置为 mysite.settings
  • (这在 Django 1.6中删除)在 python 解释器中使用 setup_environ:

    from django.core.management import setup_environ
    from mysite import settings
    
    
    setup_environ(settings)
    

Naturally, the first way is the easiest.

在你的 python shell/ipython 中做:

from django.conf import settings


settings.configure()

在运行于 python2.7.11上的 django 1.10.1中,我尝试使用 django-admin runserver而不是我的项目目录中的 manage.py runserver来启动服务器。

在 Django 1.9上,我尝试了 django-admin runserver,得到了相同的错误,但是当我使用 python manage.py runserver时,我得到了预期的结果。这可能会为很多人解决这个错误!

在我的例子中,我在尝试通过 PyCharm 运行 Django 测试时得到了这个。我认为这是因为 PyCharm 不加载最初的 Django 项目设置,即那些 manage.py shell最初运行的设置。可以将它们添加到测试脚本的开始部分,或者只是使用 manage.py test运行测试。

版本:

  • Python 3.5(在 viralenv 中)
  • PyCharm 2016.3.2专业版
  • 姜戈1.10

2017年,django 1.11.5和 python 3.6(从注释中可以看出,this 也可以用于 Python 2.7) :

import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()

将此代码放入其中的 .py应该位于 mysite(父代码)中

对于使用 IntelliJ 的用户,通过这些设置,我可以从 shell (在 Windows 上)进行查询。

enter image description here