首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Django DB设置‘配置不正确’错误

Django DB设置‘配置不正确’错误
EN

Stack Overflow用户
提问于 2013-03-22 03:36:12
回答 4查看 182.9K关注 0票数 170

Django (1.5)对我来说很好,但是当我启动Python解释器(Python3)来检查一些东西时,我得到了最奇怪的错误,当我尝试导入- from django.contrib.auth.models import User -

代码语言:javascript
复制
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设置为:

代码语言:javascript
复制
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是否配置不正确?

EN

回答 4

Stack Overflow用户

发布于 2015-07-11 10:01:56

在python shell/ipython中执行以下操作:

代码语言:javascript
复制
from django.conf import settings

settings.configure()
票数 41
EN

Stack Overflow用户

发布于 2016-09-23 02:05:53

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

票数 12
EN

Stack Overflow用户

发布于 2016-09-18 04:42:55

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15556499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档