Django 2.0中文书籍的第四章用测试template的时候出错的解决办法

本文详细介绍了在遇到Django模板设置未配置错误时的两种解决方案:导入settings配置环境变量或使用manage.py shell启动交互式解释器。通过实例演示了解决步骤及运行效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 转自http://blog.youkuaiyun.com/xiaowanggedege/article/details/8651236?utm_source=tuicool
  2. django模板报错  
  3.   
  4. Requested setting TEMPLATE_DEBUG, but settings are not configured.   
  5. You must either define the environment variable DJANGO_SETTINGS_MODULE   
  6. or call settings.configure() before accessing settings.  
  7.   
  8.   
  9. 直接python命令启动python交互式解释器,导入django template会报错  
  10.   
  11. yixiaohan@ubuntu:~/djbk$ pythonPython 2.7.3 (default, Aug 1 2012, 05:16:07)   
  12. [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.  
  13. >>> from django import template  
  14. >>> t = template.Template('my name is {{ name }}')  
  15. Traceback (most recent call last): File "<stdin>", line 1, in <module>   
  16. File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/django/template/base.py",   
  17. 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.>>>  
  18.   
  19.   
  20. 解决方法一:  
  21.   
  22. 先导入settings  
  23.   
  24. >>> from django.conf import settings  
  25.   
  26. >>> settings.configure()  
  27.   
  28. >>> from django import template  
  29. >>> t = template.Template('My name is {{ name }}.')  
  30. >>> c = template.Context({'name': 'yixiaohan'})  
  31. >>> print t.render(c)  
  32. My name is yixiaohan.  
  33. >>> c = template.Context({'name': 'xiaowangge'})  
  34. >>> print t.render(c)  
  35. My name is xiaowangge.  
  36.   
  37. 解决方法二:  
  38.   
  39. 使用python manage.py shell启动 Python交互式解释器(实际上启动的是Ipython)  
  40.   
  41. python manage.py shell  
  42.   
  43. yixiaohan@ubuntu:~/djbk$ python manage.py shell  
  44. Python 2.7.3 (default, Aug  1 2012, 05:16:07)   
  45. Type "copyright", "credits" or "license" for more information.  
  46.   
  47. IPython 0.12.1 -- An enhanced Interactive Python.  
  48. ?         -> Introduction and overview of IPython's features.  
  49. %quickref -> Quick reference.  
  50. help      -> Python's own help system.  
  51. object?   -> Details about 'object', use 'object??' for extra details.   
  52.   
  53. In [1]: from django import template  
  54.   
  55. In [2]: t = template.Template("my name is {{ name }}")  
  56.   
  57. In [3]: c = template.Context({'name':'yixiaohan'})  
  58.   
  59. In [4]: rt = t.render(c)  
  60.   
  61. In [5]: rt  
  62. Out[5]: u'my name is yixiaohan'  
  63.   
  64. In [6]: print rt  
  65. my name is yixiaohan  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值