`
JavaCrazyer
  • 浏览: 2991345 次
  • 性别: Icon_minigender_1
  • 来自: 河南
社区版块
存档分类

Django开发中DJANGO_SETTINGS_MODULE is undefined解决方案

阅读更多

相信大家在学习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
0
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics