2013-03-21 72 views
132

Django(1.5)對我來說很好,但是當我啓動Python解釋器(Python 3)來檢查一些事情時,當我嘗試導入時,出現最奇怪的錯誤 - from django.contrib.auth.models import User -Django數據庫設置'錯誤配置'錯誤

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

......這是怎麼配置不當?

+0

在設置一個新項目時遇到這個問題。我發現做'解除DJANGO_SETTINGS_MODULE'是有幫助的。我遇到了一個問題,我已經在我的.bashrc中設置了env var – fncomp 2015-10-29 00:08:19

回答

214

你不能只啓動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) 
    

當然,第一種方法是最簡單的。

+0

謝謝!這在文檔中對我來說不是很清楚,或者我可能讀過它並忘記了我需要做的時候。 – Zamphatta 2013-03-24 00:13:13

+6

奇怪的是,它似乎無法找到'mysite.settings'文件,但我可以看到它坐在那裏作爲mysite/settings.py。啊!我開始討厭Django。 – Zamphatta 2013-03-24 00:28:08

+0

對我來說也完全陌生,因爲函數具有某種靜態功能,所以爲什麼設置這個設置... – Dukeatcoding 2013-08-05 13:16:18

30

在你的Python殼/ IPython的事:

from django.conf import settings 

settings.configure() 
2

我自己的情況在Django 1.10.1上python2.7.11運行,我想在我的項目目錄使用django-admin runserver啓動服務器,而不是manage.py runserver

7

在Django 1.9上,我嘗試了django-admin runserver並得到了相同的錯誤,但是當我使用'python manage.py runserver'時,我得到了預期的結果。這可能爲很多人解決這個錯誤!

+0

相同,在Django上1.10,但試圖運行命令loaddata – amchugh89 2016-11-18 17:56:48

2

在我的情況下,我試圖通過PyCharm運行Django測試時得到了這個。我認爲這是因爲PyCharm不會加載最初的Django項目設置,即manage.py shell最初運行的項目設置。可以將它們添加到測試腳本的開頭,或者使用manage.py test運行測試。

版本:

  • 的Python 3.5(在的virtualenv)
  • PyCharm 2016年3月2日專業
  • 的Django 1.10
7

在2017年使用Django 1.11.5和python 3.6:

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

.py其中你把這個代碼應該在mysite(父親)

+0

這似乎是爲我工作,謝謝 – AbdulHamid 2017-11-01 14:39:49