14

我正在学习基本的 django 教程,并且正在使用 python manage.py runserver 运行服务器。我得到这个错误,我不太了解如何解决:

Traceback (most recent call last):
  File "manage.py", line 1, in <module>
    from polls.models import Poll
  File "/Users/davidhaddad/Desktop/mysite/polls/models.py", line 2, in <module>
    from django.db import models
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/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.

settings.py 之前是可见的,在我添加一些代码向管理员注册一些应用程序模型后,我开始收到此消息。

提供更多信息:当我从项目目录中导出 DJANGO_SETTINGS_MODULE=settings 然后我执行 python django-admin.py runserver 时,服务器没有运行(没有显示)。

4

1 回答 1

9

您需要能够导入DJANGO_SETTINGS_MODULE不在项目根目录中的位置时的值。一种简单的方法是(1)将项目文件夹的父级添加到PYTHONPATH,(2)更改DJANGO_SETTINGS_MODULEprojectfoldername.settings.

于 2012-07-15T11:22:10.637 回答