zoukankan      html  css  js  c++  java
  • django模板报错Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define

    django模板报错Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define

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

    本文由@易枭寒(499065469@qq.com)整理写成.转载请注明出处和作者信息.
  • 相关阅读:
    bzoj 1017 魔兽地图DotR
    poj 1322 chocolate
    bzoj 1045 糖果传递
    poj 3067 japan
    timus 1109 Conference(二分图匹配)
    URAL 1205 By the Underground or by Foot?(SPFA)
    URAL 1242 Werewolf(DFS)
    timus 1033 Labyrinth(BFS)
    URAL 1208 Legendary Teams Contest(DFS)
    URAL 1930 Ivan's Car(BFS)
  • 原文地址:https://www.cnblogs.com/start0cheng/p/3713744.html
Copyright © 2011-2022 走看看