相信大家在学习the django book第四章模板的时候,根据示例代码运行会出现如下这种错
具体错误我描述为两种方式形成的错误
一种是进入解释器交互环境中进行编码
F:\VideoTxSpace\mysite>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "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 "F:\Python27\lib\site-packages\django\template\base.py", line 123, in __init__
if settings.TEMPLATE_DEBUG and origin is None:
File "F:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "F:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." %
ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is und
efined.
另一种是直接在文件中编码,通过python命令运行
myiste\mysite目录下有个testTemplate.py,内容如下
from django import template
t = template.Template('my name is {{ name }}.')
c = template.Context({'name':'wujintao'})
F:\VideoTxSpace\mysite>python F:\VideoTxSpace\mysite\mysite\testTmpelate.py
Traceback (most recent call last):
File "F:\VideoTxSpace\mysite\mysite\testTmpelate.py", line 2, in <module>
t = template.Template('my name is {{ name }}.')
File "F:\Python27\lib\site-packages\django\template\base.py", line 123, in __init__
if settings.TEMPLATE_DEBUG and origin is None:
File "F:\Python27\lib\site-packages\django\utils\functional.py", line 184, in inner
self._setup()
File "F:\Python27\lib\site-packages\django\conf\__init__.py", line 40, in _setup
raise ImportError("Settings cannot be imported, because environment variable %s is undefined." %
ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is und
efined.
经实践,有三种解决方案
第一种解决方案
进入到mysite目录下,用python manage.py shell启动python解释器环境,manage.py shell命令在启动解释器之前,它告诉Django使用哪个设置文件。 Django框架的大部分子系统,包括模板系统,都依赖于配置文件;如果Django不知道使用哪个配置文件,这些系统将不能工作。
执行程序
F:\VideoTxSpace\mysite>python manage.py shell
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django import template
>>> t = template.Template('my name is {{name}}')
>>> c = template.Context({'name':'wujintao'})
>>> print t.render(c)
my name is wujintao
第二种解决方案
不用通过python manage.py shell开启解释器交互模式,直接在代码开始前中加入如下代码片段
from django.conf import settings
settings.configure()
完整代码
from django import template
from django.conf import settings
settings.configure()
t = template.Template('my name is {{ name }}.')
c = template.Context({'name':'wujintao'})
执行程序
F:\VideoTxSpace\mysite>python F:\VideoTxSpace\mysite\mysite\testTmpelate.py
my name is wujintao.
第三种解决方案
windows下设置环境变量
新增环境变量如下
添加DJANGO_SETTINGS_MODULE 值为mysite.settings
添加PYTHONPATH 值为 F:\Python27\DLLs;F:\VideoTxSpace\mysite
解释
Django搜索DJANGO_SETTINGS_MODULE环境变量,它被设置在settings.py中。例如,假设mysite在你的Python搜索路径中,那么DJANGO_SETTINGS_MODULE应该被设置为:’mysite.settings’
完整代码
from django import template
t = template.Template('my name is {{ name }}.')
c = template.Context({'name':'wujintao'})
执行程序
F:\VideoTxSpace\mysite>python F:\VideoTxSpace\mysite\mysite\testTmpelate.py
my name is wujintao.
附上我程序的结构图,以方便大家对以上方案的理解
F:\VideoTxSpace\mysite这个mysite为python项目名称
- 大小: 7.1 KB
- 大小: 7.4 KB
分享到:
相关推荐
python web开发 django最好的学习教程 django 推荐书籍
django+apache+mod_wsgi完美匹配包
使用_Django_和_Python_开发_Web_站点
Django By Example中文_目录_精排_2017_文字版,彩色 Django By Example中文_目录_精排_2017
django-calendar-master_pythondjango_calendar_django_源码
这个是对django基础编程的实践,对刚入门的人应该有很大帮助。
django平台开发视频网站源代码,包括xadmin+django+python完整项目。
ERP管理软件
calendar python django
基于django开发的博客系统,界面简单,开源代码
基于DJANGO写的BLOG源代码,支持HTML文本编辑功能
精通 Django_文字_高清_目录,django入门的精典教程...
django_step_by_step.rar django_step_by_step.rar
The_Django_Book_2.0_中文版.pdf,简单易学,可以做手册
Django Girls 教程(中文_目录_文字_精排),难得的简体中文版
DjangoBook_1.11中文_目录[20170615].pdf
Python 16、DJANGO框架 2-1_DJANGO_Day02_AM.mp4
Python 16、DJANGO框架 8-1_DJANGO_Day08_AM.mp4