首页 > 代码库 > django交互模式使用模板报:django.core.exceptions.ImproperlyConfigured
django交互模式使用模板报:django.core.exceptions.ImproperlyConfigured
(dj_01)milo@py:~/.virtualenvs/dj_01/mysite$ pwd /home/milo/.virtualenvs/dj_01/mysite (dj_01)milo@py:~/.virtualenvs/dj_01/mysite$ python >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/template/base.py", line 123, in __init__ if settings.TEMPLATE_DEBUG and origin is None: File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/conf/__init__.py", line 52, in __getattr__ self._setup(name) File "/home/milo/.virtualenvs/dj_01/lib/python2.6/site-packages/django/conf/__init__.py", line 45, in _setup % (desc, 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.
解决方法:
方法一:
(dj_01)milo@py:~/.virtualenvs/dj_01/mysite$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.environ[‘DJANGO_SETTINGS_MODULE‘] = ‘mysite.settings‘ #注意此处到mysite需要换成自己的项目名称。 >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) >>> c = template.Context({‘name‘:‘Adrian‘}) >>> print t.render(c) My name is Adrian. >>> c = template.Context({‘name‘:‘Fred‘}) >>> print t.render(c) My name is Fred.
方法二:
(dj_01)milo@py:~/.virtualenvs/dj_01/mysite$ python manage.py shell >>> from django import template >>> t = template.Template(‘My name is {{ name }}.‘) >>> c = template.Context({‘name‘:‘Adrian‘}) >>> print t.render(c) My name is Adrian. >>> c = template.Context({‘name‘:‘Fred‘}) >>> print t.render(c) My name is Fred.
本文出自 “Hello Tech” 博客,请务必保留此出处http://ninefive.blog.51cto.com/9469723/1568018
django交互模式使用模板报:django.core.exceptions.ImproperlyConfigured
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。