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 the environment variable DJANGO_SETTINGS_MODULE 
or call settings.configure() before accessing settings.


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

yixiaohan@ubuntu:~/djbk$ pythonPython 2.7.3 (default, Aug 1 2012, 05:16:07) 
[GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.
>>> from django import template
>>> t = template.Template('my name is {{ name }}')
Traceback (most recent call last): File "<stdin>", line 1, in <module> 
File "/usr/local/lib/python2.7/dist-packages/Django-1.5.dev20120922131713-py2.7.egg/django/template/base.py", 
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.>>>


解决方法一:

先导入settings

>>> from django.conf import settings

>>> settings.configure()

>>> from django import template
>>> t = template.Template('My name is {{ name }}.')
>>> c = template.Context({'name': 'yixiaohan'})
>>> print t.render(c)
My name is yixiaohan.
>>> c = template.Context({'name': 'xiaowangge'})
>>> print t.render(c)
My name is xiaowangge.

解决方法二:

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

python manage.py shell

yixiaohan@ubuntu:~/djbk$ python manage.py shell
Python 2.7.3 (default, Aug  1 2012, 05:16:07) 
Type "copyright", "credits" or "license" for more information.

IPython 0.12.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details. 

In [1]: from django import template

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

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

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

In [5]: rt
Out[5]: u'my name is yixiaohan'

In [6]: print rt
my name is yixiaohan




本文由@易枭寒(499065469@qq.com)整理写成.转载请注明出处和作者信息.
相关推荐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论 2

打赏作者

易枭寒

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值