[Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured.
来源:互联网 发布:lol mac 国服 52pj 编辑:程序博客网 时间:2024/06/06 04:36
转自http://blog.csdn.net/pleasecallmewhy/article/details/8898532
今天正在试图敲写以下代码:
- >>> from django import template
- >>> t = template.Template('My name is {{ name }}.')
- >>> c = template.Context({'name': 'WHY'})
- >>> print t.render(c) My name is WHY.
- >>> c = template.Context({'name': 'HuaHua'})
- >>> print t.render(c) My name is HuaHua.
结果报了一大堆错误:
- 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
大致的看了一下,说是什么设置没有配置,需要配置环境变量。
可以用以下方法来解决:
在import之前加入一下代码导入settings:
- >>> from django.conf import settings
- >>> settings.configure()
问题就解决了。
当然,还有另两种种解决方案,在此之前,我们先来看一下为什么不引入setting会报错。
启动python有两种方式:python manage.py shell和python。
这两个命令 都会启动交互解释器,但是manage.py shell命令有一个重要的不同: 在启动解释器之前,它告诉Django使用 哪个设置文件。Django框架的大部分子系统,包括模板系统,都依赖于配置文件;如果Django不知道使用哪个配置文件,这些系统将不能工作。
如果你想知道,这里将向你解释它背后是如何工作的。 Django搜索DJANGO_SETTINGS_MODULE环境变 量,它被设置在settings.py中。例如,假设mysite在你的Python搜索路径中,那么 DJANGO_SETTINGS_MODULE应该被设置为:’mysite.settings’。
当你运行命令:python manage.py shell,它将自动帮你处理DJANGO_SETTINGS_MODULE。 在当前的这 些示例中,我们鼓励你使用`` python manage.py shell``这个方法,这样可以免去你大费周章地去配置那些你 不熟悉的环境变量。
随着你越来越熟悉Django,你可能会偏向于废弃使用`` manage.py shell`` ,而是在你的配置文 件.bash_profile中手动添加 DJANGO_SETTINGS_MODULE这个环境变量。
所以,另两种解决方案就是:
1.使用python manage.py shell启动Python
2.在你的配置文 件.bash_profile中手动添加 DJANGO_SETTINGS_MODULE这个环境变量。
- [Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured.
- [Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured.
- django模板报错Requested setting TEMPLATE_DEBUG, but settings are not configured. You must either define
- Requested setting TEMPLATE_DEBUG, but settings are not configured.
- django中执行py报错Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured
- django.core.exceptions.ImproperlyConfigured: Requested setting TEMPLATE_DEBUG, but settings are not解
- django模板报错Requested setting TEMPLATE_DEBUG, but...
- pycharm 中的 Requested setting CACHES, but settings are not configured.
- Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configured
- Python [解决方法] error: Requested setting INSTALLED_APPS, but settings are not configured
- django.core.exceptions.ImproperlyConfigured: Requested setting CACHES, but settings are not configur
- 3_ImproperlyConfigured Requested setting DEFAULT_INDEX_TABLESPACE, but settings
- Gradle settings for this project are not configured yet
- IDEA报错URI is not registered ( Setting | Project Settings | Schemas and DTDs
- maven配置setting.xml报Could not read settings.xml
- Redis报错 MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on d
- webdriver 实例 IE8报错:Unexpected error launching Internet Explorer. Protected Mode settings are not the
- tomcat启动报错Perhaps it exists but certain dependencies are not available, 求大神
- 关于.net 项目发布到iis上后,点开aspx页面就出问题的部分解决
- mysql中左外连接结果的排序
- Spark源码分析(二)RDD
- u-boot移植详细文档
- Linux进程间通信之无名管道(PIPE)
- [Django]引入模版之后报错Requested setting TEMPLATE_DEBUG, but settings are not configured.
- Foundation框架(7)——NSArray的排序(普通排序、使用block排序、高级排序)
- JDK1.6新特性
- android 4中文件操作
- LoadLibrary返回NULL,GetLastError返回998
- iframe height=100%无效问题
- storm DRPC例子
- oracle+RAC+linux5.1所需要安装的包
- hdu4004 简单二分+贪心