背景:正常安装python和Django,python命令行执行如下命令报错
C:\Users\Heyn>python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> from django.template import Template, Context
>>> t = Template('Hello, {{ name }}')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\template\base.py", line 182, in __i
nit__
engine = Engine.get_default()
File "C:\Python27\lib\site-packages\django\utils\lru_cache.py", line 124, in w
rapper
result = user_function(*args, **kwds)
File "C:\Python27\lib\site-packages\django\template\engine.py", line 88, in ge
t_default
django_engines = [engine for engine in engines.all()
File "C:\Python27\lib\site-packages\django\template\utils.py", line 110, in al
l
return [self[alias] for alias in self]
File "C:\Python27\lib\site-packages\django\template\utils.py", line 107, in __
iter__
return iter(self.templates)
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 33, in _
_get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python27\lib\site-packages\django\template\utils.py", line 31, in tem
plates
self._templates = settings.TEMPLATES
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __ge
tattr__
self._setup(name)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 41, in _set
up
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but se
ttings are not configured. You must either define the environment variable DJANG
O_SETTINGS_MODULE or call settings.configure() before accessing settings.
>>>
解决方案:
>>> from django.conf import settings
>>> settings.configure()
>>> import django
>>> django.setup()