django 模板报错
"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."
- 先导入settings
- >>> from django.conf import settings
- >>> settings.configure()
- >>> from django import template
- >>> t = template.Template('My name is {{ name }}.')
- >>> c = template.Context({'name': 'yixiaohan'})
- >>> print t.render(c)
- My name is yixiaohan.
- >>> c = template.Context({'name': 'xiaowangge'})
- >>> print t.render(c)
- My name is xiaowangge.
- 解决方法二:
- 使用python manage.py shell启动 Python交互式解释器(实际上启动的是Ipython)
- python manage.py shell
- yixiaohan@ubuntu:~/djbk$ python manage.py shell
- Python 2.7.3 (default, Aug 1 2012, 05:16:07)
- Type "copyright", "credits" or "license" for more information.
- IPython 0.12.1 -- An enhanced Interactive Python.
- ? -> Introduction and overview of IPython's features.
- %quickref -> Quick reference.
- help -> Python's own help system.
- object? -> Details about 'object', use 'object??' for extra details.
- In [1]: from django import template
- In [2]: t = template.Template("my name is {{ name }}")
- In [3]: c = template.Context({'name':'yixiaohan'})
- In [4]: rt = t.render(c)
- In [5]: rt
- Out[5]: u'my name is yixiaohan'
- In [6]: print rt
- my name is yixiaohan