django.core.exceptions.Improperly配置有关设置TEMPLATES的信息

时间:2015-05-12 03:57:27

标签: python django

关于django.template的问题

这是代码:

from django import template
t = template.Template('My name is {{ name }}.')

但是当我跑步时:

Traceback (most recent call last):
    File "F:/daima/QPython/123.py", line 2, in <module>
      t = template.Template('my name is {{ name }}.')
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\base.py", line 187, in __init__
        engine = Engine.get_default()
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\utils\lru_cache.py", line 125, in wrapper
        result = user_function(*args, **kwds)
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\engine.py", line 73, in get_default
        django_engines = [engine for engine in engines.all()
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 108, in all
        return [self[alias] for alias in self]
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 105, in __iter__
        return iter(self.templates)
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\utils\functional.py", line 60, in __get__
        res = instance.__dict__[self.name] = self.func(instance)
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\template\utils.py", line 31, in templates
        self._templates = settings.TEMPLATES
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\conf\__init__.py", line 48, in __getattr__
        self._setup(name)
    File "C:\Python27\lib\site-packages\django-1.8-py2.7.egg\django\conf\__init__.py", line 42, in _setup
        % (desc, ENVIRONMENT_VARIABLE))
 django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

设置?出了什么问题?谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

首先导入设置。

from django.conf import settings
settings.configure()
from django import template
t = template.Template('My name is {{ name }}.')