1. django模板报错  

  2. Requested setting TEMPLATE_DEBUG, but settings are not configured.  

  3. You must either define the environment variable DJANGO_SETTINGS_MODULE  

  4. or call settings.configure() before accessing settings.  

  5. 直接python命令启动python交互式解释器,导入django template会报错  

  6. yixiaohan@ubuntu:~/djbk$ pythonPython 2.7.3 (default, Aug 1 2012, 05:16:07)  

  7. [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.  

  8. >>> from django import template  

  9. >>> t = template.Template('my name is ` name `')  

  10. Traceback (most recent call last): File "<stdin>", line 1, in <module>  

  11. File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/django/template/base.py",  

  12. line 123, in __init__ if settings.TEMPLATE_DEBUG and origin is None: File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/django/conf/__init__.py", line 50, in __getattr__ self._setup(name) File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/django/conf/__init__.py", line 43, in _setup % (name, 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.>>>  

  13. 解决方法一:  

  14. 先导入settings  

  15. >>> from django.conf import settings  

  16. >>> settings.configure()  

  17. >>> from django import template  

  18. >>> t = template.Template('My name is ` name `.')  

  19. >>> c = template.Context({'name': 'yixiaohan'})  

  20. >>> print t.render(c)  

  21. My name is yixiaohan.  

  22. >>> c = template.Context({'name': 'xiaowangge'})  

  23. >>> print t.render(c)  

  24. My name is xiaowangge.  

  25. 解决方法二:  

  26. 使用python manage.py shell启动 Python交互式解释器(实际上启动的是Ipython)  

  27. python manage.py shell  

  28. yixiaohan@ubuntu:~/djbk$ python manage.py shell  

  29. Python 2.7.3 (default, Aug  1 2012, 05:16:07)  

  30. Type "copyright", "credits" or "license" for more information.  

  31. IPython 0.12.1 -- An enhanced Interactive Python.  

  32. ?         -> Introduction and overview of IPython's features.  

  33. %quickref -> Quick reference.  

  34. help      -> Python's own help system.  

  35. object?   -> Details about 'object', use 'object??' for extra details.  

  36. In [1]: from django import template  

  37. In [2]: t = template.Template("my name is ` name `")  

  38. In [3]: c = template.Context({'name':'yixiaohan'})  

  39. In [4]: rt = t.render(c)  

  40. In [5]: rt  

  41. Out[5]: u'my name is yixiaohan'  

  42. In [6]: print rt  

  43. my name is yixiaohan