Python Django DB 设置“配置不当”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15556499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django DB Settings 'Improperly Configured' Error
提问by Zamphatta
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-
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.
How could it be improperly configured, when it works fine outside the Python interpreter? In my Django settings, the DATABASESsettings are:
当它在 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.
}
}
...how is this improperly configured?
...这是如何不正确配置的?
采纳答案by Pavel Anossov
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:
你不能只是启动 Python 并检查事情,Django 不知道你想从事什么项目。您必须执行以下操作之一:
- Use
python manage.py shell - Use
django-admin.py shell --settings=mysite.settings(or whatever settings module you use) - Set
DJANGO_SETTINGS_MODULEenvironment variable in your OS tomysite.settings (This is removed in Django 1.6) Use
setup_environin the python interpreter:from django.core.management import setup_environ from mysite import settings setup_environ(settings)
- 用
python manage.py shell - 使用
django-admin.py shell --settings=mysite.settings(或您使用的任何设置模块) - 将
DJANGO_SETTINGS_MODULE操作系统中的环境变量设置为mysite.settings (这在 Django 1.6 中被删除)
setup_environ在 python 解释器中使用:from django.core.management import setup_environ from mysite import settings setup_environ(settings)
Naturally, the first way is the easiest.
当然,第一种方式是最简单的。
回答by Montaro
In your python shell/ipython do:
在你的 python shell/ipython 中:
from django.conf import settings
settings.configure()
回答by Dev
in my own case in django 1.10.1 running on python2.7.11, I was trying to start the server using django-admin runserverinstead of manage.py runserverin my project directory.
在我自己的情况下,在 python2.7.11 上运行的 django 1.10.1 中,我试图使用django-admin runserver而不是manage.py runserver在我的项目目录中启动服务器。
回答by Jacob Young
On Django 1.9, I tried django-admin runserverand got the same error, but when I used python manage.py runserverI got the intended result. This may solve this error for a lot of people!
在 Django 1.9 上,我尝试django-admin runserver并得到了同样的错误,但是当我使用时python manage.py runserver我得到了预期的结果。这可能会为很多人解决这个错误!
回答by Deleet
In my case, I got this when trying to run Django tests through PyCharm. I think it is because PyCharm does not load the initial Django project settings, i.e. those that manage.py shellruns initially. One can add them to the start of the testing script or just run the tests using manage.py test.
就我而言,我在尝试通过 PyCharm 运行 Django 测试时得到了这个。我认为这是因为 PyCharm 没有加载初始 Django 项目设置,即那些manage.py shell最初运行的设置。可以将它们添加到测试脚本的开头,或者只使用manage.py test.
Versions:
版本:
- Python 3.5 (in virtualenv)
- PyCharm 2016.3.2 Professional
- Django 1.10
- Python 3.5(在 virtualenv 中)
- PyCharm 2016.3.2 专业版
- Django 1.10
回答by MagTun
In 2017 with django 1.11.5 and python 3.6 (from the comment this also works with Python 2.7):
在 2017 年使用 django 1.11.5 和 python 3.6(从评论中这也适用于 Python 2.7):
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
The .pyin which you put this code should be in mysite(the parent one)
在.py其中你把这个代码应该是在mysite(父之一)


