-4

为什么这段代码不起作用?

from django.template import Template,Context
t = Template('Hello , {{name}}')
for name in ('Jack' , 'Sara' , 'John'):
    print t.render(Context({'name' : name})) 
4

1 回答 1

3

如果您直接在本机 Python 解释器会话中键入它,它将不起作用;事实上它引发了一个ImproperlyConfigured例外:

In [4]: t = Template('Hello , {{name}}')
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (38, 0))

---------------------------------------------------------------------------
ImproperlyConfigured                      Traceback (most recent call last)
...

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.

另一方面,如果您使用正确加载所需设置的命令运行 shell 会话,它确实有效。django-admin.pymanage.py

于 2013-05-10T08:46:26.527 回答