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."

  1. 先导入settings  
  2.   
  3. >>> from django.conf import settings  
  4.   
  5. >>> settings.configure()  
  6.   
  7. >>> from django import template  
  8. >>> t = template.Template('My name is {{ name }}.')  
  9. >>> c = template.Context({'name': 'yixiaohan'})  
  10. >>> print t.render(c)  
  11. My name is yixiaohan.  
  12. >>> c = template.Context({'name': 'xiaowangge'})  
  13. >>> print t.render(c)  
  14. My name is xiaowangge.  
  15.   
  16. 解决方法二:  
  17.   
  18. 使用python manage.py shell启动 Python交互式解释器(实际上启动的是Ipython)  
  19.   
  20. python manage.py shell  
  21.   
  22. yixiaohan@ubuntu:~/djbk$ python manage.py shell  
  23. Python 2.7.3 (default, Aug  1 2012, 05:16:07)   
  24. Type "copyright", "credits" or "license" for more information.  
  25.   
  26. IPython 0.12.1 -- An enhanced Interactive Python.  
  27. ?         -> Introduction and overview of IPython's features.  
  28. %quickref -> Quick reference.  
  29. help      -> Python's own help system.  
  30. object?   -> Details about 'object', use 'object??' for extra details.   
  31.   
  32. In [1]: from django import template  
  33.   
  34. In [2]: t = template.Template("my name is {{ name }}")  
  35.   
  36. In [3]: c = template.Context({'name':'yixiaohan'})  
  37.   
  38. In [4]: rt = t.render(c)  
  39.   
  40. In [5]: rt  
  41. Out[5]: u'my name is yixiaohan'  
  42.   
  43. In [6]: print rt  
  44. my name is yixiaohan  
原文地址:https://www.cnblogs.com/kfx2007/p/3417436.html