尝试将数据添加到数据库时出错

时间:2013-02-04 17:04:50

标签: django visual-studio python-2.7

尝试使用Python Tool for Visual Studio运行shell命令时收到以下错误。我已将数据库添加到设置文件中,并且能够无错误地运行django应用程序,但是当我尝试使用shell添加数据时,它会抛出此错误:

>>> from ProjectTrackerServer.projects.models import Project
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\djangoapps\ProjectTrackerServer\ProjectTrackerServer\projects\models.py", line 1, in <module>
    from django.db import models
  File "C:\Python27\lib\site-packages\django\db\__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "C:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
    self._setup()
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.

1 个答案:

答案 0 :(得分:1)

错误说明了问题。之所以发生这种情况,是因为您忘记提供项目设置DJANGO_SETTINGS_MODULE。您可以通过提供它来修复它。您需要设置DJANGO_SETTINGS_MODULE环境变量。

When you use Django, you have to tell it which settings
you're using. Do this by using an environment variable, 
DJANGO_SETTINGS_MODULE.

The value of DJANGO_SETTINGS_MODULE should be in 
Python path syntax, e.g. mysite.settings. Note 
that the settings module should be on the 
Python import search path.

https://docs.djangoproject.com/en/dev/topics/settings/