在终端使用模板
from django import template
t = template.Template('My name is {{ name }}.')
提示错误
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Python/2.7/site-packages/django/template/base.py", line 123, in __init__
if settings.TEMPLATE_DEBUG and origin is None:
File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/Library/Python/2.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.
不接 google 然后找到如下答案
If you're playng with Django directly from Python, and not from Django's own manage.py shell, then you need to set the DJANGO_SETTINGS_MODULE variable before launching Python.
For example, if you have a Django project named "myproject", then run the following before launching Python:
export DJANGO_SETTINGS_MODULE=myproject.settings
For Apache to pick up that variable, you need to set it in httpd.conf as follows:
SetEnv DJANGO_SETTINGS_MODULE myproject.settings
意思就是
在命令行下使用python命令进入交互模式的,Template(str)在构照模板的时候依赖django.conf.settings.这个变量,但是在当前的环境中没有这个变量,这时,它就会抛出异常了
办法就是 要想使用django的模板你必须导入settings
>>> from django.conf import settings
>>> settings.configure()
你在运行t = Template("my name is {{name}}") 就不会出错了
还有一种方法就是使用python manager.py shell运行
manager.py在项目目录下
现在就一切正常
参考链接 http://iyouf.info/fixed-django-settings-error.html
http://forum.webfaction.com/viewtopic.php?id=2282