(dj_01)[email protected]:~/.virtualenvs/dj_01/mysite$ pwd /home/milo/.virtualenvs/dj_01/mysite (dj_01)[email protected]:~/.virtualenvs/dj_01/mysite$ python >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/template/base.py", line 123, in __init__ if settings.TEMPLATE_DEBUG and origin is None: File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/conf/__init__.py", line 52, in __getattr__ self._setup(name) File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/conf/__init__.py", line 45, in _setup % (desc, ENVIRONMENT_VARIABLE)) django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
解决方法:
方法一:
(dj_01)[email protected]:~/.virtualenvs/dj_01/mysite$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘mysite.settings‘ #注意此处到mysite需要换成自己的项目名称。 >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) >>> c = template.Context({‘name‘:‘Adrian‘}) >>> print t.render(c) My name is Adrian. >>> c = template.Context({‘name‘:‘Fred‘}) >>> print t.render(c) My name is Fred.
方法二:
(dj_01)[email protected]:~/.virtualenvs/dj_01/mysite$ python manage.py shell >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) >>> c = template.Context({‘name‘:‘Adrian‘}) >>> print t.render(c) My name is Adrian. >>> c = template.Context({‘name‘:‘Fred‘}) >>> print t.render(c) My name is Fred.
时间: 2024-12-19 06:58:14