2013-03-21 13 views
132

Django (1.5) funziona perfettamente per me, ma quando accendo l'interprete Python (Python 3) per controllare alcune cose, ottengo l'errore più strano quando provo a importare - from django.contrib.auth.models import User -Errore Django DB impostazioni 'impropriamente configurate'

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. 

Come potrebbe essere configurato in modo improprio, quando funziona ben al di fuori dell'interprete Python? Nelle mie impostazioni Django, le impostazioni DATABASES sono:

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

... come è questa non configurato correttamente?

+0

Si è imbattuto in questo quando si imposta un nuovo progetto. Ho trovato utile fare "disinserire DJANGO_SETTINGS_MODULE". Stavo incontrando un problema in cui avevo impostato l'env var nel mio .bashrc – fncomp

risposta

214

Non è possibile attivare Python e controllare le cose, Django non sa su quale progetto si desidera lavorare. Devi fare una di queste cose:

  • Usa python manage.py shell
  • Usa django-admin.py shell --settings=mysite.settings (o qualsiasi modulo impostazioni si utilizza)
  • impostare la variabile DJANGO_SETTINGS_MODULE ambiente nel vostro sistema operativo per mysite.settings
  • (Questo è stato rimosso in Django 1.6) Utilizzare setup_environ nell'interprete python:

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

Naturalmente, la prima è la più semplice.

+0

Grazie!Non mi era chiaro nella documentazione, o forse l'ho letto e dimenticato dal momento in cui avevo bisogno di farlo. – Zamphatta

+6

Stranamente non sembra che trovi il file 'mysite.settings', ma posso vederlo seduto proprio lì come mysite/settings.py. Ugh! Sto iniziando a odiare Django. – Zamphatta

+0

completamente strano anche a me, perché la funzione ha qualche tipo di funzione statica, quindi perché configurare queste impostazioni ... – Dukeatcoding

30

nella shell Python/ipython fare:

from django.conf import settings 

settings.configure() 
2

nel mio caso in Django 1.10.1 esecuzione su python2.7.11, stavo cercando di avviare il server utilizzando django-admin runserver invece di manage.py runserver nella mia directory del progetto .

7

Su Django 1.9, ho provato django-admin runserver e ho ottenuto lo stesso errore, ma quando ho usato "python manage.py runserver" ho ottenuto il risultato desiderato. Questo potrebbe risolvere questo errore per molte persone!

+0

same, on django 1.10, ma durante il tentativo di eseguire il comando loaddata – amchugh89

2

Nel mio caso, ho ottenuto questo quando si tenta di eseguire i test di Django tramite PyCharm. Penso che sia perché PyCharm non carica le impostazioni iniziali del progetto Django, cioè quelle che manage.py shell esegue inizialmente. È possibile aggiungerli all'inizio dello script di test o eseguire semplicemente i test utilizzando manage.py test.

Versioni:

  • Python 3.5 (in virtualenv)
  • PyCharm 2016/03/02 professionale
  • Django 1,10
7

Nel 2017 con Django e Python 1.11.5 3.6:

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

.py in cui si inserisce questo codice deve essere in mysite (quello principale)

+0

questo sembra funzionare per me, grazie – AbdulHamid

Problemi correlati