如何修复Django配置不当的异常?

如何修复Django配置不当的异常?,django,python-2.7,Django,Python 2.7,自从学习Django以来,我一直在虚拟环境中使用pip跟踪Django的安装。当Django的书告诉我在Python shell中键入这个时 from django import template t = template.Template("My name is {{ name }}.") 我得到了这个例外。我不知道怎么解决这个问题 Traceback (most recent call last): File "<stdin>", line 1, in <module

自从学习Django以来,我一直在虚拟环境中使用pip跟踪Django的安装。当Django的书告诉我在Python shell中键入这个时

from django import template
t = template.Template("My name is {{ name }}.")
我得到了这个例外。我不知道怎么解决这个问题

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "django/template/base.py", line 123, in __init__
    if settings.TEMPLATE_DEBUG and origin is None:
  File "django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "django/conf/__init__.py", line 46, in _setup
    % (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG,
but settings are not configured. You must either define the environment variable
DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“django/template/base.py”,第123行,在__
如果settings.TEMPLATE_DEBUG and origin为无:
文件“django/conf/_init__.py”,第53行,在_getattr中__
自我设置(名称)
文件“django/conf/_init__.py”,第46行,在_设置中
%(描述,环境变量)
django.core.exceptions.ImpropertlyConfigured:请求的设置模板\u调试,
但未配置设置。您必须定义环境变量
DJANGO_SETTINGS_模块或调用SETTINGS.configure(),然后访问设置。

有人能带我过去吗?我使用的是Mac OS X 10.8

您需要运行django shell,以便它加载设置

python manage.py shell

您似乎正在使用命令
python
运行pythonshell。因此,
DJANGO\u设置\u模块
变量不可用。使用

python manage.py shell
阅读更多关于

manage.py设置DJANGO_SETTINGS_MODULE环境变量,使其指向项目的SETTINGS.py文件


Django需要特定于应用程序的设置。 因为它已经在manage.py中,所以只需运行:

python manage.py shell


这应该足够快地解决问题

这是否也意味着我的系统python shell没有Django?或者,Django shell是否专门从
manage.py
运行?您的系统python shell将具有Django特定的设置。但是,如果希望项目特定的设置(如
TEMPLATE\u DEBUG
DEBUG
)正常工作,则需要使用
python manage.py shell
打开shell。运行
python manage.py shell
时,我会遇到该错误(配置不正确)。。。有什么想法吗?。。。这快把我逼疯了!