Python Template 错误
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
出现这个错误问题的原因正如错误报告所写:无法导入 Settings, DJANGO_SETTINGS_MODULE 环境变量没有定义。有以下几种解决方法:
1). 最简单的解决办法是在项目或app当前目录下,进入python命令界面时,使用python manager.py shell 代替 python。因为Django manager.py 会使用模板加载器自动查找当前目录下的settings.py 信息,加载器文件中包含from django.conf import settings导入所需的django settings配置,就不会出错了:
$ python manage.py shell
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>>
2). 若使用>>> python命令,则通过自己从正确的包中导入settings并手动配置也可以:
$ python
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import template
>>> from django.conf import settings
>>> settings.configure()