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.
Run Code Online (Sandbox Code Playgroud)
如果它在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.
}
}
Run Code Online (Sandbox Code Playgroud)
......这是如何配置不当的?
Pav*_*sov 259
你不能只是启动Python并检查事物,Django不知道你想要做什么项目.你必须做以下事情之一:
python manage.py shelldjango-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)
Run Code Online (Sandbox Code Playgroud)当然,第一种方式是最简单的方法.
Mon*_*aro 38
在你的python shell/ipython中:
from django.conf import settings
settings.configure()
Run Code Online (Sandbox Code Playgroud)
Mag*_*Tun 25
2017年,django 1.11.5和python 3.6:
import django
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
Run Code Online (Sandbox Code Playgroud)
在.py其中你把这个代码应该是在mysite(父之一)
小智 10
在Django 1.9上,我尝试了django-admin runserver并得到了同样的错误,但是当我使用'python manage.py runserver'时,我得到了预期的结果.这可能会为很多人解决这个错误!
| 归档时间: |
|
| 查看次数: |
166192 次 |
| 最近记录: |