为什么这段代码不起作用?
from django.template import Template,Context
t = Template('Hello , {{name}}')
for name in ('Jack' , 'Sara' , 'John'):
print t.render(Context({'name' : name}))
答案 0 :(得分: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.
另一方面,如果你run your shell session using django-admin.py
or manage.py
commands正确加载了所需的设置,它确实有效。