1

a question about django.template

here is code:

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

but when i ran:

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.

settings?what's wrong?thanks for your help

evbo
  • 157
  • 4
  • 14
  • Maybe you're missing the `TEMPLATE_DIRS` setting? – spalac24 May 12 '15 at 04:02
  • but it's just a python program,not in django frame.what is that? – evbo May 12 '15 at 04:21
  • Is this an isolated program? Not a full django project? I don't think django was made for that. – spalac24 May 12 '15 at 04:23
  • just a python program with django.template,not in django frame. – evbo May 12 '15 at 04:28
  • possible duplicate of [How do I use Django templates without the rest of Django?](http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django) – spalac24 May 12 '15 at 04:40
  • i see it just now,but what does "settings.configure()" means? – evbo May 12 '15 at 04:53
  • Basically, it tells Django to set up everything in order to be used. Including the templating functions. You cannot do anything with Django if you do not do this first. However, also, if you're just going to use templating, consider using other, more powerful, template engines such as `mako` or `jinja2`. – spalac24 May 12 '15 at 04:57

1 Answers1

0

Import settings first.

from django.conf import settings
settings.configure()
from django import template
t = template.Template('My name is {{ name }}.')
Yihe
  • 4,094
  • 2
  • 19
  • 21